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?

3
  • 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? Commented Jul 29, 2015 at 21:17
  • so in the "real" you are still using classes ? Commented Jul 29, 2015 at 21:18
  • There is no assigment of a field, it's nonsense. Values are being assigned. A value may be a reference. Commented Jul 29, 2015 at 21:44

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

I don't have access to the index, nor the original array instance upon assignment, sadly. But thanks for pointing out the possiblity.
#2 does not require access to the array, but only to the object stored in an array position.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.