Fortunately, you can let the Framework do the copying for you. One overloaded constructor for an ArrayList accepts another ICollection, and all elements it contains are *copied* to the new ArrayList. So, instead of assigning one list to the other like this:
Code:
this.arrCopy = arr;
simply create a new ArrayList and pass in the old version:
Code:
this.arrCopy = new ArrayList(arr);
The following code is a working example that shows you what I mean:
Code:
// Create two List Instances
ArrayList List1 = new ArrayList();
ArrayList List2 = new ArrayList();
// Declare 3rd list for Constructor demo
ArrayList List3;
// Add items to List1
List1.Add("Hi");
List1.Add("Bye");
// Assign List1 to List2
List2 = List1;
// Change second item in second list
// This should also affect List1
List2[1] = "New Bye";
// Both Message boxes should return New Bye
MessageBox.Show(String.Format("List1[1] is now {0}", List1[1]));
MessageBox.Show(String.Format("List2[1] is now {0}", List2[1]));
// Now use the overloaded contructor to
// *copy* the elements in List1 to List3
List3 = new ArrayList(List1);
// Change second item in third list. Was New Bye
// Is now Old Bye. Will *not* affect List1
List3[1] = "Old Bye";
// Now the MessageBox for List1 will return New Bye (the old value)
// while the listbox for List3 will return Old Bye (the new value)
MessageBox.Show(String.Format("List1[1] is now {0}", List1[1]));
MessageBox.Show(String.Format("List3[1] is now {0}", List3[1]));
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.