For some reason I cannot seem to identify what I am doing wrong. I have tried a few things and each time the original record is modified.
I have a list of a particular class. I am iterating through the list, if a specific condition is met, I need to create a duplicate item for this iteration, modify 2 values, then add it to the final list. Except everytime I modify the duplicate I created, the values are modified in the original. The data is a binding list that is set to a list.
// MyClasses is a BindingList<MyClass>
List<MyClass> classesToSave = new List<MyClass>();
foreach (var class in MyClasses)
{
classesToSave(class);
if (class.NeedsDupe)
{
MyClass dupeClass = new MyClass();
dupeClass = class;
dupeClass.NeedsDupe = false;
dupeClass.IsDupe = true;
classesToSave(dupeClass);
}
}
Lets say that through the first iteration, the first item in MyClasses has class.NeedsDupe == true and class.IsDupe == false. When class is added to classesToSave before the condition is hit, the only entry in classesToSave is the original class (class.NeedsDupe == true and class.IsDupe == false). However, when the condition is hit and the dupeClass object is modified and before dupeClass is added to classesToSave, the original entry (and only entry) in classesToSave is then modified.
Am I missing something completely? I have a feeling that I am drawing a brain fart and maybe need a reference to the old object? Or am I somehow copying the properties of the BindingList and when I change the dupeClass object that I think is a new object I am actually causing the change to ripple back through?
Thanks for any help.