I have:
instance1 = instance2;
how do I disconnect them from one another, so that altering one will not affect the other?
EDIT: I want them referencing the same object (so I can’t clone), and later – not. But I still want both instances of the class – so I can’t ‘null’ them.
Thanks
EDIT:
myclass a = new myclass();
a.integer = 1;
myclass b = new myclass();
b.integer = 2;
a = b;
//All changes to one will affect the other, Which is what I want.
//<More lines of the program>
//Now I want 'a' to point to something else than 'b'. and I’m missing the code
//so that the next line will not affect 'b'.
a.integer = 1;
Text = b.integer.ToString();
//I need b.integer to still be = 2, it’s not.
With:
class myclass
{
public int integer;
}
EDIT:
This is the answer:
@ispiro but when you say a.integer = 1 you aren’t changing the pointer, you are following the pointer and changing the value at the end of it. – Davy8
I had thought that changing both ‘a’ and ‘a.integer’ would be the same in the sense that changing them would either change pointer-‘a’ or won’t. But in fact: the first does, the second doesn’t.
Thanks everyone.
So in the example above, if I add:
a = c;// where c is another instance of 'myclass'.
It will change ‘a’ to point somewhere else than ‘b’.
But:
a.integer = 1;
did not.
Your code:
If you keep around a reference:
Variables are labels to the objects, not the objects themselves. In your case, the original
ano longer has a reference to it so even though it exists, there’s no way to access it. (and it’ll cease to exist whenever the garbage collector gets around to getting rid of it unless there are other references to it)If it helps, think of it this way, when you say
a = bora = new myclass()you are moving the line whereais pointing. When you saya.integer = 1, the.is kind of like saying follow the lineais pointing to, then change the destination.