Question

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.

Was it helpful?

Solution

That's the nature of classes, they are reference type. Create a copy constructor for MyClass

class MyClass
{
   public MyClass(MyClass myClass)
   {
       //Assign the properties here
       //this.x = myClass.x;
       //....
       //....
   }
}

then you call

MyClass dupeClass = new MyClass(@class);

OTHER TIPS

The following lines are problematic. dupeclass references the class; they point to the same object. Instead of making them equal, set the properties of dupeClass equal to the properties of class.

MyClass dupeClass = new MyClass();
dupeClass = class;

In this line you're creating a new instance of MyClass:

MyClass dupeClass = new MyClass();

but then you're immediately overwriting it with the original:

dupeClass = class;

from then on, dupeClass and class are both referencing the same object, so changes to dupeClass are also made to class.

you are creating a new instance and then assigning the old one to it

MyClass dupeClass = new MyClass();
        dupeClass = class;

UPDATE You have to either copy the properties by hand or use Reflection

foreach (var property in oldObject.GetType().GetProperties())
            {
                var prop= newObject.GetType().GetProperty(property.Name);
                if (prop!= null)
                    prop.SetValue(newObject, property.GetValue(oldObject));
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top