user194076
user194076

Reputation: 9017

Assign object by reference

I have two viewState properties.

One is a List<MyObject> MyObjects
And another one is MyObject SelectedObject

Whenever user selects an object from the list, I assign it to SelectedObject like this:

SelectedObject = MyObjects.Where(x=>x.MyId ==IdSelectedInUI)  

This allows me easy manipulation of the SelectedObject.
Now, When I change SelectedObject's properties, how Can I assign those properties back to the original object in the list? I was thinking to use ref, but cannot quite figure it out.

Edit All of the objects are classes, not structs.
So, here's what I have:

//Children are of type =List<MyParentObject.ChildObject>
 public MyParentObject MyParent
        {
            get
            {
                if (ViewState["MyParent"] == null)
                {
                    MyParent= GetItemsFromDatabase();
                }
                return (TaskInfo)ViewState["MyParent"];
            }

            set
            {
                ViewState["MyParent"] = value;
            }
        }

        public MyParentObject.ChildObject SelectedChild
        {
            get
            {
                return (MyParentObject.ChildObject)ViewState["Child"];
            }

            set
            {
                ViewState["Child"] = value;
            }
        }  
        protected onButtonClick()
        {
          SelectedChild =  MyParent.Children.Where(x=>x.Child_Id == Convert.ToInt32(txt.Text)).FirstOrDefault();
         SelectedChild.Property2 = "test2" //OldValue "test1"
         //now if I run 
        MyParent.Children.Where(x=>x.Child_Id ==Convert.ToInt32(txt.Text)).Select(x=>x.Property2) //Shows "test1" and I expect "test2"
        }
        }

Upvotes: 0

Views: 209

Answers (2)

Akintunde
Akintunde

Reputation: 56

It depends. Assuming MyObject is a class, it is passed "by value", but the value being passed is a reference to the original object.

For instance:

void Mutate(SomeObject x)
{
     x.val = "banana";
}
void Reassign(SomeObject x)
{
     x = new SomeObject();
     x.val = "Garbage";
}
public static void Main()
{
     SomeObject x = new SomeObject();
     x.val = "Apple";
     Console.WriteLine(x.val); // Prints Apple
     Mutate(x);
     Console.WriteLine(x.val); // Prints banana
     Reassign(x);
     Console.WriteLine(x.val); // Still prints banana
}

This is how passing objects works in C#. You can modify the contents, but not the "value" of the object itself (which is a pointer to it in memory).

If you are looking to change the contents of the object, nothing special needs to be done.

Passing an object by ref parameter will allow you to reassign the reference to it. For example, if:

void Reassign(ref SomeObject x) // implementation

Was used in the previous example, the print out of the final WriteLine would have been "Garbage", as the reference itself was changed, and due to the ref parameter, this changed the reference in the caller as well.

+Edit If MyObject is a struct, however, the structure is copied when passed, and reassigning it inside a different scope will have no effect on the original structure.

What you could do for this is to have a function return the structure and assign it:

MyStruct Mutate(MyStruct x){}
// calling
myStruct = Mutate(myStruct);

Or do it by reference parameter.

Upvotes: 0

prprcupofcoffee
prprcupofcoffee

Reputation: 2970

SelectedObject is an object reference. It is identical with the original object in the list - SelectedObject and the "original" object are the same object. If you are seeing behavior that indicates otherwise, please update your question.

Upvotes: 2

Related Questions