I am reading a .csv file and loading each record that I read into a class then populating the class into an array list. The issue I have is that the class contains strings and seems to be copied by reference in the array list. Therefore causing an issue when adding additional records the reference is just being written multiple times in the arraylist with the same pointer. A friend suggested I use a dataset instead of an arraylist. I have to do sorting and filtering in which I would need to write functions for. I am not sure how to clone the class into the arraylist so that each entry is unique by value and not by reference point. Any help on how to load each record so that I can do some processing would be greatly appreciated.
Thanks in advance
Ben

ArrayList copying a class
xtega
I tried what you suggested and it didnt work.
public
void addCust(){
Customer cust2 =
new Customer();cust2 = cust;
client.Add(cust2);
}
the class customer has the following variables in it.
private
string name = null; private string dateofPurch = null; private string nameStreet1 = null; private string nameStreet2 = null; private string city = null; private string state = null; private string zip = null; private string email = null; private string amount = null; private string balance = null;What is happening is that the arraylist has two entries. Both are the same as the last entry in the table.
GGanesh
Thanks for your help.. I will try that in the morning. To see if that works.
Kiranboi
dr4nra
I understand that, but because it's a class, it's a reference type. So your call to new is useless, that new object is discarded and a new reference to your existing object is made. You can either make customer a struct ( which it looks like to me ), or you can copy the string values across between the two objects,rather than make them refer to the same object.
Maigo
kangur
CrispinH
Well, mostly if it's just a container for data, strings and numbers and such, it's a struct. If it has a lot of methods and state and so on, it's a class.
The key difference is that a struct is passed by value, so your = operation would make a copy. A class does not do that, it makes a reference.
nop
No problem - if you have more trouble, post in this thread again, it will bump up so folks can see it, and I'll get an email so I can try to help some more.
Murph the Surf
// cust2 = cust;
What is this line for It's negating and reversing my solution. You're adding the same customer every time, unless cust is reset somewhere. Try making customer a struct, it looks like it should be one anyhow.
Otherwise, you need to copy your values across without changing what object you have a reference to.
Andro_
Rewrite your inserting code so it inserts a new instance of your class every time. This just means putting the code that creates an instance of the class and calls new for it inside whatever loop you've written.
jimble
for (int i=0;i < 100; ++i)
{
myStruct s = new myStruct();
s.int = i;
myArrayList.Add(s);
}
This will create a new struct instance on each iteration, and add those to the arraylist. As the arraylist has a reference to each object, losing the reference in s does not matter ( and in fact, it would be lost when it went out of scope anyhow ).
myStruct s = new myStruct();
for (int i=0;i < 100; ++i)
{
s.int = i;
myArrayList.Add(s);
}
I'm not sure actually if myStruct needs to be a class for this to be a problem, but certainly if it was a class, this creates one instance and adds it over and over again to the arraylist, as you're describing.