1

I have defined a list of objects as List<NewLayerCounterMethod> NewLayer = new List<NewLayerCounterMethod>(); where each object within the class NewLayerCounterMethod has four properties such as: VehicleType, VehicleCounter,Counter,Updating.

The number of objects NewLayer is 10 and I want to copy NewLayer[7] into NewLayer[5] such that if NewLayer[7] is changed, NewLayer[5] will not be changed.

Please note that I do not want to make a new object while I want to replace the properties of one object with those of another object which makes it somehow different from other questions on Stackoverflow and I think it is not a duplication.

Bugs
  • 4,491
  • 9
  • 32
  • 41
Jackson
  • 13
  • 2
  • So you want to have a new variable which is _not_ a new object, but does not change the properties of the other object when one of its own properties is changed? – CompuChip Apr 10 '17 at 12:32
  • You can create event for NewLayerCounterMethod . When changed value you can access all data – Erdem Köşk Apr 10 '17 at 12:36
  • @CompuChip Actually I do not want to create a new variable or object and I want to reassign one of the object. I have coded in MATLAB before and in MATLAB `=` do this while in C# `=` is reference type and any change in one of objects will affect the other. I – Jackson Apr 10 '17 at 12:40
  • C# does a copy by default for value types. If you want value semantics, use a value type (change your `class` into a `struct`). Make sure you understand the implications, though. – Luaan Apr 10 '17 at 12:43
  • @Luaan You mean if I change Class to Struct It works? – Jackson Apr 10 '17 at 12:53
  • Yes, but note that this applies to all cases where you pass a value type (e.g. as an argument to a function) - so make sure to pass the values by reference where needed. And note that it *is* creating a new object, though the implications of that depend a lot on what you're doing. – Luaan Apr 10 '17 at 12:59

2 Answers2

1

You need to clone the object, as otherwise you would just store the reference and in both fields and change them both. See this answers:

Creating a copy of an object in C#

How do you do a deep copy of an object in .NET (C# specifically)?

Community
  • 1
  • 1
Kris
  • 512
  • 7
  • 16
  • Thanks but I think the recommended approaches make a new object. However, I want to re-assign `NewLayer[5]` – Jackson Apr 10 '17 at 13:26
0

In that case you need to add a function to your NewLayerCounterMethod class, in which you copy all the properties of the other class to the new one. It would look somewhat like that then:

public void TakeProperties(NewLayerCounterMethod otherObject)
{
    propA = otherObject.propA;
    propB = otherObject.propB;
    //And so on for all properties left
}

You could add guards so that you only update the properties if the new value is different to the old one, but I suggest to add those guards directly to the properties.

The above method is than called like that:

NewLayer[5].TakeProperties(NewLayer[7]);
MetaColon
  • 2,895
  • 3
  • 16
  • 38