none
HOw to create a totaly separate instance from an existing one RRS feed

  • Question

  • Dear all,

    I have a  list of ViewModelCar form which I display in a list box.

    My need is to create a totally new instance based on an existing ViewModelCar Instance and get that new instance totally independent.

    What is happening so far is that where I create a new instance, if I change a property value in the new created instance, it is also affecting the source ViewModel.

    What is the way to create that new instance to avoid this ?

    I would like to avoid creating the instance and update all properties one by one

    Thanks' for advise

    regards 

    Thursday, March 29, 2018 6:51 AM

Answers

  • An example of your code would help, but it sounds like all you are doing is copying one reference variable into another one like this:

    ViewModelCar car1 = new ViewModelCar();
    // set car1 properties
    
    ViewModelCar car2 = car1;
    // set car2 properties

    Is this what you are doing?

    If this is the case it would explain what you are seeing. This is because any variable of an object datatype such as ViewModelCar is a reference type. See here for an explanation of the difference between value types (such as int and double) and reference types. Or just do an internet search for "C# value types vs reference types".

    Basically, when you create car1 using the 'new' keyword you are really allocating some memory on the heap that holds the data and 'pointing' the car1 variable at it.

    When you set car2 = car1 all you are doing is copying that reference from car1 into car2. So now you have two variables 'pointing' at the same area of heap memory. This is why, if you change a property on car1 you will see the change reflected in car2 (or vice versa), because they are referring to the same object.

    If you want to create a copy, I'm afraid it is up to you to provide that functionality. The most obvious way being to provide a Clone() method in your ViewModelCar (there is an ICloneable interface you can implement for this).

    E.g

    public ViewModelCar: ICloneable
    {
        public object Clone()
        {
           ViewModelCar clone = new ViewModelCar();
           clone.MyProperty1 = this.MyProperty1;
           // etc etc
           return clone;
        }
    }

    If your ViewModelCar contains sub-objects that you need cloned then you will need to do that as well unless they are built-in framework classes that already have a Clone() method.

    I don't think there is any easy shortcut to avoid this.


    • Edited by RJP1973 Thursday, March 29, 2018 7:29 AM
    • Marked as answer by wakefun Thursday, March 29, 2018 7:47 AM
    Thursday, March 29, 2018 7:28 AM

All replies

  • An example of your code would help, but it sounds like all you are doing is copying one reference variable into another one like this:

    ViewModelCar car1 = new ViewModelCar();
    // set car1 properties
    
    ViewModelCar car2 = car1;
    // set car2 properties

    Is this what you are doing?

    If this is the case it would explain what you are seeing. This is because any variable of an object datatype such as ViewModelCar is a reference type. See here for an explanation of the difference between value types (such as int and double) and reference types. Or just do an internet search for "C# value types vs reference types".

    Basically, when you create car1 using the 'new' keyword you are really allocating some memory on the heap that holds the data and 'pointing' the car1 variable at it.

    When you set car2 = car1 all you are doing is copying that reference from car1 into car2. So now you have two variables 'pointing' at the same area of heap memory. This is why, if you change a property on car1 you will see the change reflected in car2 (or vice versa), because they are referring to the same object.

    If you want to create a copy, I'm afraid it is up to you to provide that functionality. The most obvious way being to provide a Clone() method in your ViewModelCar (there is an ICloneable interface you can implement for this).

    E.g

    public ViewModelCar: ICloneable
    {
        public object Clone()
        {
           ViewModelCar clone = new ViewModelCar();
           clone.MyProperty1 = this.MyProperty1;
           // etc etc
           return clone;
        }
    }

    If your ViewModelCar contains sub-objects that you need cloned then you will need to do that as well unless they are built-in framework classes that already have a Clone() method.

    I don't think there is any easy shortcut to avoid this.


    • Edited by RJP1973 Thursday, March 29, 2018 7:29 AM
    • Marked as answer by wakefun Thursday, March 29, 2018 7:47 AM
    Thursday, March 29, 2018 7:28 AM
  • thanks
    Thursday, March 29, 2018 7:47 AM