0

I have a situation where I get a reference to an object that I would like to "overwrite" with another object of the same type. I know by design that the objects are of the same type. I do not have access to the parent object in this function.

The dataobject is defines as like:

    Class DataObject
    {
         public List<int> Stuff = new List<int>();
    }

Then there is a method

    void DoStuff(object obj)
    {
         // At this point I know that obj is List<int>
         // Create new object from some source
         var newList = new List<int>();

         // Here I would like to make the passed object the new object
         (the pointer of obj) = (the pointer of newlist)
    }

I don't know if this is possible. It's just something I've been banging my head against for a couple of hours now and my brain seems to have stopped working.

Thanks in advance

Johan

Johan Karlsson
  • 898
  • 1
  • 10
  • 24
  • You are not acttly just trying to replace the contents of Stuff are you? eg `void DoStuff() { this.Stuff = new List; } – Bob Vale Jul 08 '11 at 10:38

4 Answers4

0

You could use the Clone function but take a look at the following Stackoverflow link that has a nice method for cloning objects.

Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24
0

Pass object by reference:

void DoStuff(ref DataObject dataObject)
{
    dataObject = ...;
}

ref object won't be allowed here, however.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

No, it's not possible with your current code - the reference is passed by value, and is thus entirely independent of how you originally accessed it. Even if you changed obj to be a ref parameter, you wouldn't be able to call the method with ref Stuff because the types would be incorrect. You could potentially do it with a generic parameter:

void DoStuff<T>(ref T obj)
{
    object newList = new List<int>();
    obj = (T) newList;
}

That's pretty horrible though.

It's normally a design smell to want to do this - if you can explain more about the bigger picture, we may be able to help more.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have a Command object model that allows for piping commands together. A command is of type CommandBase where the T:s are the model. I use delegates to pipe commands to different parts of the model if the signatures of the commands doesn't match. That is if A -> B with data A.Outdata.List -> B.Indata I map them using CommandA.PipeResultTo().MapInput(e=>e.Outdata.List). At the point of writing that statement, e.Outdata is null since the previous command hasn't been executed yet. – Johan Karlsson Jul 08 '11 at 10:52
0

obj = newList what difference it will make if pointer of obj is changed to newList or pointer of newList will be changed to obj?

hungryMind
  • 6,931
  • 4
  • 29
  • 45
  • Because obj is a member of the main DTO. If you do the above the main object will still reference to old one and obj will just be a reference to the new one. – Johan Karlsson Jul 08 '11 at 10:53