Okay, I have what I think is a simple question.. or just a case of me being a C# beginner.
I have an array of custom objects (clsScriptItem) that I am populating from a database. Once the items are loaded, I want to back them up to "backup" array so I can revert the information back after changing the main array. However, when I use CopyTo to copy the array and then alter the original array, the backup array is also being altered... I thought CopyTo merely copied values + structure from one array to another.
private void backupItems()
{
//lastSavedItems and items are both of type clsScriptItem[]
//(declaration not shown)
lastSavedItems = new clsScriptItem[items.Length];
items.CopyTo(lastSavedItems, 0);
//items[0].nexts[0] is 2
//lastSavedItems[0].nexts[0] is 2
items[0].nexts[0] = "-1";
//items[0].nexts[0] is -1
//lastSavedItems[0].nexts[0] is also -1
}
How do I backup this data without having the two arrays be 'linked'??
UPDATE : I have updated the backup function to this
private void backupItems()
{
lastSavedItems = new clsScriptItem[items.Length];
for (int i = 0; i < items.Length; i++)
lastSavedItems[i] = (clsScriptItem)items[i].Clone();
items[0].nexts[0] = "-1";
}
And I have update my class thusly....
public class clsScriptItem : ICloneable
{
//other declarations...
object ICloneable.Clone() { return Clone(); }
public clsScriptItem Clone()
{
return ((clsScriptItem)MemberwiseClone());
}
}
** - and the same thing is happening.. Any thoughts?**