1

I'm writting a piece of code (c#) in windows phone 8 (had the same issue with windows 8).

And I am wondering, how to passe value of one object and not his reference.

Let me explain with one exemple :

public MyClass
{
    private Foo foo //my object.

    public void Init()
    {
        foo = new Foo();
        foo.age = 5; 
        ChangeFooValue(foo);
    }
    private void ChangeFooValue(Foo temp)
    {
        temp.age = 10; 
        //I want to change temp and NOT foo.
        //But at the end of this 
        //foo.age = 10;
        //and
        //temp.age = 10;
    }
}

Solved : I had this in my class to create a deep copy :

public Foo DeepCopy()
        {
            Foo other = (Foo) this.MemberwiseClone();
            return other;
        }

ps: It's maybe a dumb question (if it is, please, provide me some tutorial to be able to resolve it by my self).

David
  • 1,679
  • 11
  • 22
  • temp is actually "passed by value", it is just a reference type and hence the behaviour. You should clone the passed object. – Styxxy Mar 15 '13 at 09:24
  • why not use a struct instead of class for your Foo type? – mbm Mar 15 '13 at 09:36

2 Answers2

7

Reference type's address is passed by value, that is why you are seeing this effect. You may create a deep copy of your object before passing to the function.

You should see: Parameter passing in C# by Jon Skeet

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • but deep copying will create a new object – TalentTuner Mar 15 '13 at 09:24
  • @Saurabh, yes that is the only way when you don't want the method to modify the class members – Habib Mar 15 '13 at 09:24
  • it may be possible that he just need to change a part of the object so creating full fledged object , looks not a ideal choice for the OP, Habib answer is correct but i am just thinking is it not better to just create an anonymous object and initialize only those property rather than creating a full object – TalentTuner Mar 15 '13 at 09:30
  • @Saurabh, I guess the code in question is probably an example, original code may involve more fields. But still if the requirement is to pass an object of that class and that object shouldn't get modified, then I think one has to create deep copy of the object – Habib Mar 15 '13 at 09:32
  • @Habib: Your answer is correct but i am just thinking is it not better to just create an anonymous object and initialize only those property rather than creating a full object – TalentTuner Mar 15 '13 at 09:35
  • @Saurabh, yes , if the object doesn't have to be modified at all, then I guess it shouldn't be passed to the method in first place. – Habib Mar 15 '13 at 09:37
  • Thank you, you gave me the way to find to solution. – David Mar 15 '13 at 09:42
  • @David, you are welcome, Remember to go through the article, it is one of the best by Jon – Habib Mar 15 '13 at 09:46
0

I prefer the other answer but there is another approach you could use to dupe the object, using an overloaded constructor. It is described here:

http://www.codeproject.com/Articles/14686/C-Parameter-Pass-object-by-value-The-copy-construc

From there you could pass like so

ChangeFooValue( new Foo(foo) );
stevepkr84
  • 1,637
  • 2
  • 13
  • 23
  • 2
    That will require `Foo` to have copy constructor implemented, and that again is deep copying – Habib Mar 15 '13 at 09:32