PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How to stop a variable from changing as and when the assigned variable value changes C#

 July 06, 2022     c#, pass-by-reference, pass-by-value, variables     No comments   

Issue

I have two ObservableCollection variables temp and original of ObservableCollection datatype. I have assigned original = temp at some point. temp collection keeps changing with different values at different times. I want original to change only when I do : original = temp specifically and rest all the times it should hold the old assigned value and should not change to temp as and when it changes . How can I achieve this in C#. Please help


Solution

The easiest way is to do a deep copy of original when you create temp. By default, copying any complex object will just do a shallow copy of just the object reference (basically it makes your new variable point to the same spot as the old variable) so any changes will be reflected in both variables. What you want is a new object with the same data. The easiest way to achieve this is to do a deep copy with Json

public static T Clone<T>(T baseObject)
{
    return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(baseObject));
}

In your code when you create temp, and whenever you set original to be the value of temp, use the clone method instead of just setting 'original=temp'. The above code uses json to turn your object into its json string representation, and then create a completely independent object using said string

Note, this method isn't always applicable:

The classes you are using might have an inbuilt deep copy method. For example XElement will let you pass an XElement to its constructor for deep copying, if the class you are using has such a method - said method will likely be optimised better than just using Json to deep copy. Most classes will work with json, but depending on the class it can be safer to use the explicit deep copy method.

Your class might also be self referencing, lack an empty constructor, contain other classes with those properties or it might have some other property that makes json conversion problematic. If that's the case you might want to create your own deep copy method. Basically just add another constructor to your class that takes in an instance of itself - in the constructor copy across each simple property, and for each complex property either jsonCopy the property across or handle it however works best in your use case. You normally won't need to go this far, but for very complex classes you might have to. You could also create a 'json Friendly' class which can convert to and from your problematic class and work that way.



Answered By - The Lemon
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing