views:

134

answers:

1

I have a delegate that modifies an object. I pass an object to the delegate from a calling method, however the calling method does not pickup these changes. The same code works if I pass a List as the object. I thought all objects were passed by reference so any modifications would be reflected in the calling method?

I can modify my code to pass a ref object to the delegate but am wondering why this is necessary?

public class Binder
{
    protected delegate int MyBinder<T>(object reader, T myObject);

    public void BindIt<T>(object reader, T myObject)
    {
        //m_binders is a hashtable of binder objects
        MyBinder<T> binder = m_binders["test"] as MyBinder<T>;
        int i = binder(reader, myObject);
    }
}

public class MyObjectBinder
{
    public MyObjectBinder()
    {
        m_delegates["test"] = new MyBinder<MyObject>(BindMyObject);
    }

    private int BindMyObject(object reader, MyObject obj)
    {
        obj = new MyObject
        {
            //update properties
        };
        return 1;
    }
}

///calling method in some other class
public void CallingMethod()
{
    MyObject obj = new MyObject();

    MyObjectBinder binder = new MyObjectBinder();
    binder.BindIt(myReader, obj); //don't worry about myReader

    //obj should show reflected changes
}

UPDATED

I passed objects by ref to the delegate as I was instantiating a new object inside BindMyObject.

protected delegate int MyBinder<T>(object reader, ref T myObject);
+10  A: 

Objects aren't passed by reference. Objects aren't passed at all.

By default, the value of the argument is passed by value - whether that value is a value type value or a reference. If an object is modified via that reference, then that change will be visible to the calling code as well.

In the code you showed originally, there was no reason to use ref. The ref keyword is used when you want a method that changes the value of a parameter (e.g. to make it refer to a different object entirely) and have that change visible to the caller.

Now, in the code you've shown (originally) you've only got:

private int BindMyObject(object reader, MyObject obj)
{
    //make changes to obj in here
}

Do you mean code like this:

private int BindMyObject(object reader, MyObject obj)
{
    obj = new MyObject();
}

or code like this:

private int BindMyObject(object reader, MyObject obj)
{
    obj.SomeProperty = differentValue;
}

? If it's the latter, then you don't need ref. If it's the former, then you do need ref because you're changing the parameter itself, not making changes to the object that the value refers to.

If you can show a short but complete program which demonstrates your problem, it'll be a lot easier to explain what's going on.

It's hard to do this topic justice in just a few paragraphs - so I've got an entirely article about it, which will hopefully make things more obvious.

Jon Skeet
+1 - I think I learned a lot of this from your book or your article
JonH
Thanks for the updated answer. Yes, the problem was because in the BindMyObject I was initializing a new object. Just found this out debugging myself.
David Liddle
@David Liddle - You might want to include the changed code in your original topic that way people see what you ended up using. So I take it you DID end up using REF being that your code new'ed a new myObject?
JonH
@JonH: I've modified my answer a little bit to make it clearer now that the code has been changed :)
Jon Skeet