0

Basically, I get a reference to a field of an object that is stored inside an array and put that into a variable.

Then, I want to assign a different object reference to the field by assigning it to the variable I stored the field in earlier.

To illustrate my point, here's a simple example:

    class myClass
    {
        public object obj = null;
    }

    class someOtherClass
    {  }

...

    static void Main(string[] args)
    {
        myClass[] arr = new myClass[] { new myClass() };
        var objVar = arr[0].obj;
        objVar = new someOtherClass();
    }

I get the field from the object from the array and store it in a variable.

Now, I want to store a new value in the field by assigning the variable.

What actually happens though is that the variable does not keep the reference to the field, but rather just assigns the object of someOtherClass to the variable, removing the field from the varible.

So after this code executed, field obj of the myClass instance is still null, while the variable objVar contains a reference to my someOtherClass instance.

Is there an easy way to assign a reference to a reference inside a variable like this?

nilllzz
  • 205
  • 3
  • 14
  • 4
    I think you may be thinking about pointers in C when you reference that way, but would it be out of the question to just write `arr[0].obj = new someOtherClass();` instead? – Ron Beyer Jul 29 '15 at 21:17
  • so in the "real" you are still using classes ? – Tigran Jul 29 '15 at 21:18
  • There is no assigment of a field, it's nonsense. Values are being assigned. A value may be a reference. – Ondrej Tucny Jul 29 '15 at 21:44

3 Answers3

3

You could use a trick with a lambda expression:

var setter = x => arr[0].obj = x;
setter(new someOtherClass());

You could even use a generic wrapper class, something like this:

class Reference<T> {

    private Action<T> setter;
    private Func<T> getter;

    public Reference(Action<T> setter, Func<T> getter) {
        this.setter = setter;
        this.getter = getter;
    }

    public T Value {
        get {
            return getter();
        }
        set {
            setter(value);
        }
    }
}

And then:

var objVar = new Reference<object>(t => arr[0].obj = t, () => arr[0].obj);
objVar.Value = new someOtherClass();

See fiddle here: https://dotnetfiddle.net/HXJJQf

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
2

I see these possibilities:

  1. Store the index:

    int i = 0;
    arr[i].obj new someOtherClass();
    
  2. Store the object whose property you want to change

    var objVar = arr[0]; // NOT arr[0].obj !
    objVar.obj = new someOtherClass();
    
  3. If you want to choose the property dynamically, i.e. the property is not always the same, then use @Blorgbeard's answer.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • I don't have access to the index, nor the original array instance upon assignment, sadly. But thanks for pointing out the possiblity. – nilllzz Jul 29 '15 at 21:57
  • #2 does not require access to the array, but only to the object stored in an array position. – Olivier Jacot-Descombes Jul 29 '15 at 22:15
  • True, however I would not have access to the object at the array position either, only the field stored in a variable. I solved it with a hybrid of your tips and @Blorgbeard's answer. – nilllzz Jul 29 '15 at 23:36
0

I think you need to copy the value, implementing something like this: Deep cloning objects

Community
  • 1
  • 1
Rolando Retana
  • 412
  • 5
  • 16