Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# - Made one instance of a class equal to another. – How to cancel that?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code:

myclass a = new myclass();
a.integer = 1;

myclass b = new myclass();
b.integer = 2;

a = b;

//here I need code to disconnect them

a.integer = 1;

Text = b.integer.ToString();
//I need b.integer to still be = 2

enter image description here

If you keep around a reference:

myclass a = new myclass();
a.integer = 1;

myclass b = new myclass();
b.integer = 2;

var c = a; //Keep the old a around
a = b;

//here I need code to disconnect them
a = c; //Restore it.

a.integer = 1;

Text = b.integer.ToString();
//It's still 2 now.

enter image description here

Variables are labels to the objects, not the objects themselves. In your case, the original a no 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 = b or a = new myclass() you are moving the line where a is pointing. When you say a.integer = 1, the . is kind of like saying follow the line a is pointing to, then change the destination.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...