Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Typed Array Question 2

Status
Not open for further replies.

spons

Programmer
Mar 17, 2004
18
US
I am using a typed array i.e. ACHS[] where ACHS is a class I defined. I need to add functions for Add and Remove. I need to have functionality like AddAT and removeAT, is there an easier way to do this with the collections namespace or anything? Currently I search the Array for an ID that I need to remove, Grab the Index, and then create a new array without the item in the index i grabbed. I know there is a more efficiant way to do this.. Suggestions ?
 
Arrays are usually (but not always) a fixed size. So if you want to add and/or remove members, they usually aren't able to support this.

Try using something like a Hashtable, which does have the add/remove methods, and can grow/shrink in size as needed.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
... Or, another method of doing what you're asking is to not use an array of objects, but a Collection of the object. To do this, keep your base class (ACHS) and add another class (maybe ACHSCollection?). Inherit from System.Collections.CollectionBase in the ACHSCollection class. By doing this, you will then be able to implement the Add, Remove, etc. methods of a collection (I hope this all makes sense to you. For example, your ACHSCollection class might look something like this:

Code:
public class ACHSCollection : System.Collections.CollectionBase
{
// This is your collection item accessor
public ACHS this[int index]
{
    get { return this[index]; }
    set { this[index] = value; }
}

// Implement the Add() method for the collection
public int Add(ACHS achsObj)
{
    return List.Add(achsObj);
}

// Implement the Remove() method for the collection
public void Remove(ACHS achsObj)
{
    List.Remove(achsObj);
}
}

By inheriting from the System.Collections.CollectionBase class, you can also benifit from other methods that are native to collections, such as Contains(), IndexOf(), etc.

Hope this all helps (and makes sense) =D

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Actually I read chiph's FAQ on Collections and found my new best friend, The Arraylist. All I did was change it from an Object array to an array list and Add and Remove, with removeAT was built in.
Code:
	public class ACHItems
	{
		public decimal TotalAmount=0;
		public System.Collections.ArrayList ACHS=new ArrayList();
		public ACHItems()
		{
		
		}
		private void Add(ACHItem item)
		{
		this.TotalAmount=TotalAmount+item.Amount;
		ACHS.Add(item);
		}
		
		private void Remove(int index)
		{
		this.TotalAmount=TotalAmount-((ACHItem)ACHS[index]).Amount;
		ACHS.RemoveAt(index);
		}
		public bool AddOrRemove(int myACHId,decimal Amount)
		{
			bool myFound=false;
			for (int i=0;i==ACHS.Count-1;i++)
			{
				ACHItem item=(ACHItem)ACHS[i];
				if (item.ACHId==myACHId)
				{
					Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return false;
			else
			{
				Add(new ACHItem(myACHId,Amount));
				return true;
			}
		}
		public bool Exists(int myACHId)
		{
			bool myFound=false;
			for (int i=0;i==ACHS.Count-1;i++)
			{
				ACHItem item=(ACHItem)ACHS[i];
				if (item.ACHId==myACHId)
				{
					//Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return true;
			else
			{
				//Add(new ACHItem(myACHId,Amount));
				return false;
			}
		}
	}

Now im just having slight problems when there are more than one row in my grid. The whole point of this class is this, I Fill A grid with the fields Customer,EFT,ACH. EFT and ACH start out RED, When the user clicks them, they turn green and are added to the Arraylist. But when there is more than one row, Its not working properly. I have had this headache for 3 days now. Maybe you guys can help me with this, the grid component i am using is TrueDBGrid from componentOne. Here is a complete listing of my code.


Code:
		private void fillGrid()
		{
			System.Data.SqlClient.SqlConnection conn=new System.Data.SqlClient.SqlConnection(NN.Tool.Data.Common.ConnectionString);
			conn.Open();
			string sql = "select Customers.CustomerId,Customers.Description as Customer,EFT.Amount as EFT,sum(Checks.Amount) as DirectDeposit,'Note' as Note,EFT.EFTId,ACHBATCHS.ACHBatchId as ACHId from ToolCheckBatches inner join AchBatchs on AchBatchs.ToolCheckBatchId = ToolCheckBatches.ToolCheckBatchId inner join AchBatchItems on AchBatchItems.AchBatchId = AchBatchs.AchBatchId inner join Invoices on Invoices.InvoiceId = ToolCheckBatches.InvoiceId full outer join EFT on EFT.InvoiceId = Invoices.InvoiceId inner join Customers on Customers.CustomerId = ToolCheckBatches.CustomerId inner join ToolChecks on ToolChecks.ToolCheckId=AchBatchItems.ToolCheckId inner join Checks on Checks.CheckId=ToolChecks.CheckId group by Customers.CustomerId,Customers.Description,EFT.Amount,EFT.EFTId,AchBatchs.AchBatchId";
			System.Data.SqlClient.SqlDataAdapter Adp=new System.Data.SqlClient.SqlDataAdapter(sql,conn);
			Adp.Fill(this.eftAndACH1.Tables[0]);
			conn.Close();
			Adp.Dispose();
		}

		private void c1TrueDBGrid1_RowColChange(object sender, C1.Win.C1TrueDBGrid.RowColChangeEventArgs e)
		{
			C1.Win.C1TrueDBGrid.Style rStyle = new C1.Win.C1TrueDBGrid.Style();
			C1.Win.C1TrueDBGrid.Style gStyle = new C1.Win.C1TrueDBGrid.Style();
			rStyle.BackColor=System.Drawing.Color.Red;
			gStyle.BackColor=System.Drawing.Color.Green;
			if (c1TrueDBGrid1.Col==2)
			{
				//EFT
				if (eftItems.AddOrRemove(Convert.ToInt32(this.eftAndACH1.Tables[0].Rows[this.c1TrueDBGrid1.Row][5]),Convert.ToDecimal(this.eftAndACH1.Tables[0].Rows[this.c1TrueDBGrid1.Row][2])))
				{
					c1TrueDBGrid1.Splits[0].DisplayColumns[c1TrueDBGrid1.Col].AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell,gStyle);

				}
				else
				{
					c1TrueDBGrid1.Splits[0].DisplayColumns[c1TrueDBGrid1.Col].AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell,rStyle);
				}
			}
			if (c1TrueDBGrid1.Col==3)
			{
				//ACH
				if (achItems.AddOrRemove(Convert.ToInt32(this.eftAndACH1.Tables[0].Rows[this.c1TrueDBGrid1.Row][6]),Convert.ToDecimal(this.eftAndACH1.Tables[0].Rows[this.c1TrueDBGrid1.Row][3])))
				{
					c1TrueDBGrid1.Splits[0].DisplayColumns[c1TrueDBGrid1.Col].AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell,gStyle);

				}
				else
				{
					c1TrueDBGrid1.Splits[0].DisplayColumns[c1TrueDBGrid1.Col].AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell,rStyle);
				}			
			}

			this.c1TrueDBGrid1.Col=1;
			this.UpdateAmounts();
		}
		private ACHItems achItems = new ACHItems();
		private EFTItems eftItems = new EFTItems();
		private void UpdateAmounts()
		{
		this.textBox1.Text=eftItems.TotalAmount.ToString("c");
		this.textBox2.Text=achItems.TotalAmount.ToString("c");
		}
		private void c1TrueDBGrid1_FetchCellStyle(object sender, C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs e)
		{
			C1.Win.C1TrueDBGrid.Style rStyle = new C1.Win.C1TrueDBGrid.Style();
			C1.Win.C1TrueDBGrid.Style gStyle = new C1.Win.C1TrueDBGrid.Style();
			rStyle.BackColor=System.Drawing.Color.Red;
			gStyle.BackColor=System.Drawing.Color.Green;
			if (e.Col==2)
			{
				//EFT
				if (eftItems.Exists(Convert.ToInt32(this.eftAndACH1.Tables[0].Rows[e.Row][5])))
				{
				e.CellStyle.BackColor=System.Drawing.Color.Green;

				}
				else
				{
				e.CellStyle.BackColor=System.Drawing.Color.Red;
				}
			}
			if (e.Col==3)
			{
				//ACH
				if (achItems.Exists(Convert.ToInt32(this.eftAndACH1.Tables[0].Rows[e.Row][6])))
				{
					e.CellStyle.BackColor=System.Drawing.Color.Green;
				}
				else
				{
					e.CellStyle.BackColor=System.Drawing.Color.Red;
				}			
			}
		}

		private void c1TrueDBGrid1_Click_1(object sender, System.EventArgs e)
		{
			//if(c1TrueDBGrid1.Col==2 || c1TrueDBGrid1.Col==3)
				//MessageBox.Show("Hi");
			
			
		}

		private void c1TrueDBGrid1_ColEdit(object sender, C1.Win.C1TrueDBGrid.ColEventArgs e)
		{
			
		}

		private void c1TrueDBGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//this.c1TrueDBGrid1.invo
			
		}
	}
	public class ACHItem
	{
		public int ACHId;
		public decimal Amount;
		public ACHItem(int myACHId,decimal myAmount)
		{
			ACHId=myACHId;
			Amount=myAmount;
		}
	}
    public class EFTItem
	{
		public int EFTId;
		public decimal Amount;
		public EFTItem(int myEFTId,decimal myAmount)
		{
		EFTId=myEFTId;
		Amount=myAmount;
		}
	}
	public class ACHItems
	{
		public decimal TotalAmount=0;
		public System.Collections.ArrayList ACHS=new ArrayList();
		public ACHItems()
		{
		
		}
		private void Add(ACHItem item)
		{
		this.TotalAmount=TotalAmount+item.Amount;
		ACHS.Add(item);
		}
		
		private void Remove(int index)
		{
		this.TotalAmount=TotalAmount-((ACHItem)ACHS[index]).Amount;
		ACHS.RemoveAt(index);
		}
		public bool AddOrRemove(int myACHId,decimal Amount)
		{
			bool myFound=false;
			for (int i=0;i==ACHS.Count-1;i++)
			{
				ACHItem item=(ACHItem)ACHS[i];
				if (item.ACHId==myACHId)
				{
					Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return false;
			else
			{
				Add(new ACHItem(myACHId,Amount));
				return true;
			}
		}
		public bool Exists(int myACHId)
		{
			bool myFound=false;
			for (int i=0;i==ACHS.Count-1;i++)
			{
				ACHItem item=(ACHItem)ACHS[i];
				if (item.ACHId==myACHId)
				{
					//Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return true;
			else
			{
				//Add(new ACHItem(myACHId,Amount));
				return false;
			}
		}
	}
	public class EFTItems
	{
		public decimal TotalAmount=0;
		public System.Collections.ArrayList EFTS=new ArrayList();
		public EFTItems()
		{
	
		}
		private void Add(EFTItem item)
		{
		this.TotalAmount=TotalAmount+item.Amount;
		EFTS.Add(item);
		}
		
		private void Remove(int index)
		{
		this.TotalAmount=TotalAmount-((EFTItem)EFTS[index]).Amount;
		EFTS.RemoveAt(index);		
		}
		
		public bool AddOrRemove(int myeftId,decimal Amount)
		{
			bool myFound=false;
			for (int i=0;i==EFTS.Count-1;i++)
			{
				EFTItem item=(EFTItem)EFTS[i];
				if (item.EFTId==myeftId)
				{
					Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return false;
			else
			{
				Add(new EFTItem(myeftId,Amount));
				return true;
			}
			
		}
		public bool Exists(int myeftId)
		{
			bool myFound=false;
			for (int i=0;i==EFTS.Count-1;i++)
			{
				EFTItem item=(EFTItem)EFTS[i];
				if (item.EFTId==myeftId)
				{
					//Remove(i);
					myFound=true;
				}
			}
			if(myFound)
				return true;
			else
			{
			//	Add(new EFTItem(myeftId,Amount));
				return false;
			}
			
		}
	}
 
Glad you got your Array question solved, but asking a question about a grid in a thread titled "Typed Array Question" probably won't get the readership you need (people who know grids).

Can you repost that part in another thread with a new title?

Thanks.
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
sorry about that, I figured I would post here since it was semi relevant, either way if you look at my code, I was getting the for loops completly wrong. i changed == to != and removed the -1 from count and it works perfect. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top