L
lukasz
I've got some code which very simplified looks as below:
public class Referenced {
public int x;
public Referenced(int val) { x = val;}
}
public class References {
public Referenced pointer;
}
public static void Main() {
Referenced ref1 = new Referenced(3);
Referenced ref2 = new Referenced(-2);
References r = new References();
r.pointer = ref1;
Console.WriteLine(r.pointer.x); // 3 -- correct
ref1 = ref2;
Console.WriteLine(ref1.x); // -2 -- correct
Console.WriteLine(r.pointer.x); // 3 -- wrong, wanted -2
}
The problem is, I want to substitute ref1 with ref2, and all variables
elsewhere referring previously to ref1 shall now refer to (point at)
ref2. So in the above sample I would like to call "ref1 = ref2" and
expect that "r.pointer == ref2" now. Is there a way to solve this
problem in C#, without resorting to real pointers?
lukasz
public class Referenced {
public int x;
public Referenced(int val) { x = val;}
}
public class References {
public Referenced pointer;
}
public static void Main() {
Referenced ref1 = new Referenced(3);
Referenced ref2 = new Referenced(-2);
References r = new References();
r.pointer = ref1;
Console.WriteLine(r.pointer.x); // 3 -- correct
ref1 = ref2;
Console.WriteLine(ref1.x); // -2 -- correct
Console.WriteLine(r.pointer.x); // 3 -- wrong, wanted -2
}
The problem is, I want to substitute ref1 with ref2, and all variables
elsewhere referring previously to ref1 shall now refer to (point at)
ref2. So in the above sample I would like to call "ref1 = ref2" and
expect that "r.pointer == ref2" now. Is there a way to solve this
problem in C#, without resorting to real pointers?
lukasz