Holding ArrayLists in an ArrayList
-
Hi,i try to hold a number arraylists in a single main arraylist.But i think because of a referance problem, everytime i add a new arraylist, previous added lists became same with last added one;all items shown as the last arraylist's items.is anyone knows the solution or a different way to hold arraylists? Thanks
-
Hi,i try to hold a number arraylists in a single main arraylist.But i think because of a referance problem, everytime i add a new arraylist, previous added lists became same with last added one;all items shown as the last arraylist's items.is anyone knows the solution or a different way to hold arraylists? Thanks
Array is an object. You are trying something like: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); ... a1.Add(a2); // thesame array should be: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); a2=new ArrayList(); ... a1.Add(a2); // another array Hi, AW
-
Array is an object. You are trying something like: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); ... a1.Add(a2); // thesame array should be: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); a2=new ArrayList(); ... a1.Add(a2); // another array Hi, AW
Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3
-
Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3
You're just copying references around. When you write
ALSendedList=ALSendedL;
you're merely copying the reference to the array list, you are not copying the array list itself. You need to think about copying the items in the array list instead. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3
... or use CopyTo() method (not Clone()) Hi, AW