0

I pass an 'global' object as a param to a method. I then init a new instance of the object and set it equal to the original, in my head i now have 2 instances of that object.

Why when i change the second instance does the first instance change? I never used 'ref'.

namespace myNamespace
{
    public class myClass
    {
        private myObjectClass myGlobalInstance;

    public void myMethod()
    {
        doSomething(myGlobalObject);    
    }

    private String doSomthing(myObjectClass myObjectInstance)
    {
            myObjectClass newObject = myObjectInstance;
        newObject.variable1 = "boo"; //this seems to change both newObject.variable1 as required  AND myObjectInstance.variable1 and its calling classes object
    }
    }
}
Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • myObjectClass newObject = myObjectInstance; that sets a reference to you myObjectInstance. Try the [ReferenceEquals](https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+ReferenceEquals) function, it'll return true if both object are the same object in memory – Nitro.de Feb 05 '16 at 15:30
  • 1
    because its reference copy, not shallow or deep copy – Szer Feb 05 '16 at 15:30
  • That's the difference between Value- and ReferenceTypes ... –  Feb 05 '16 at 15:32
  • Can you show me how to fix please? – Fearghal Feb 05 '16 at 15:33

3 Answers3

3

You are not initializing a new object. You are just copying a reference to the original one here:

myObjectClass newObject = myObjectInstance;

There is not necessarily a clean solution to your problem. Depending on what your class/object looks you might be able to use MemberwiseClone. But since this is a protected method, you'll have to expose it on myObjectClass something like so:

public myObjectClass ShallowCopy()
{
   return (myObjectClass) this.MemberwiseClone();
}

And then call it in myClass:

myObjectClass newObject = myObjectInstance.ShallowCopy();

This will make a shallow copy of your object which may or may not accomplish what you want. From the docs:

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

If that is not sufficient, then you might have to implement a clone method to do a deep copy. How to do this is also spelled out in the above docs.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
1

The RemoteConfigMgr class is a reference type, meaning that each of the variables of this type are just references (addresses) to the actual object. The line:

myObjectClass newObject = myObjectInstance;

Means that newObject variable will now refer to the same object as the myObjectInstance variable. So when you modify newObject, you are also modifying myObjectInstance.

DimitarD
  • 117
  • 1
  • 5
0

You are copying reference to object thats the problem. To create another object with the same properties you could use MemberwiseClone:

public class GlobalObject
{
    public GlobalObject GetCopy()
    {
        return (GlobalObject)MemberwiseClone();
    }
}

But you have to understand the difference between ShallowCopy and DeepCopy

Your code will look like this:

namespace myNamespace
{
    public class myClass
    {
        private GlobalObject myGlobalInstance;

        public void myMethod()
        {
            doSomthing(myGlobalObject);
        }

        private String doSomthing(GlobalObject myObjectInstance)
        {
            GlobalObject newObject = myObjectInstance.Copy();
            newObject.variable1 = "boo";
        }
    }
}
Community
  • 1
  • 1
Szer
  • 3,426
  • 3
  • 16
  • 36
  • Yes, and you have to understand the definition of an answer. Not one word to the question WHY... – TomTom Feb 05 '16 at 15:37
  • @TomTom It is a tool. Its up to you how and why use it. – Szer Feb 05 '16 at 15:38
  • Yeah, Ignorane demonstrated. TRY READING HIS QUESTION. He asks for an explanation, not a workatround. – TomTom Feb 05 '16 at 15:43
  • thx TomTom and Szer. It took me a while to understand what you were talking about but once i hit the links and read it up it was ok. This seems an extreme fix though. I have been working in Java recently and didnt hit this nor did i hit it in c# until i used a 'global' var – Fearghal Feb 05 '16 at 15:56