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.


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

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.

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.


Suppose you write "1600 Pennsylvania Avenue" on a piece of paper. That address refers to a house, in fact, the White House.

Suppose you write "1600 Pennsylvania Avenue" on a second piece of paper. That address refers to the same house as the first piece of paper.

There is absolutely nothing you can do to alter the fact that those two addresses refer to the same house. You can change one of the pieces of paper to say "123 Sesame Street", and then they no longer refer to the same house, but that isn't changing a fact about The White House, that's changing a fact about a piece of paper.

If you then paint the White House so that it is blue, you still haven't changed anything about either piece of paper; both now refer to a blue house.

Can you explain in more detail what it is that you're trying to do here? Your question is very confusing.


I think I understand better what you are saying based on your comments.

The answer is very very simple.

This code:

instance1 = instance2;

Does not link instance1 and instance2 in any way other than the fact that they point to the same thing. You can always re-point either variable (including "Nulling" them) without affecting the other. Setting either instance to null doesn't change the object it just makes it stop pointing at the object in memory.

In essence, the premise of the question is inaccurate. There is no problem here to solve.

Key point to understand: The variables instance1 and instance2 just store the location of an object in memory, not the actual object. If you wanted to manipulate the actual object you would use a method or property on that object, for example:

instance1.ChangeColor; // call the ChangeColor method on the object pointed to by instance1.

Update:

Assume a and b are index cards with the address of a mailbox that can hold a number. Here is a play-by-play of what your code is doing

//create a mailbox (call it X)  and write the address on index card A
myclass a = new myclass();  

//put the number 1 in the mailbox at the address written on index card A (currently x)
a.integer = 1;   //mailbox x now contains 1

//create ANOTHER mailbox (call it Y) and write the address on index card B
myclass b = new myclass(); 

//put the number 2 in the mailbox at the address written on index card B (currently Y) 
b.integer = 2;  //mailbox y now contains 2

// change the address on index card A to the address from card B (currently Y)
a = b;  //both cards now have the address of mailbox Y (which contains 2) written on them. 

// if you want a to go back to having the address of mailbox X, you are
// out of luck because you don't have it written down on any of your cards anymore.

//put the number 1 in the mailbox at the address pointed to on Card A (Currently Y)
a.integer = 1;  //mailbox Y now contains 1

//Set text to the number from the mailbox at the address on card B (currently Y)
Text = b.integer.ToString();  
链接地址: http://www.djcxy.com/p/79366.html

上一篇: 创建不适用于虚拟基类的对象的克隆

下一篇: 创建一个与另一个类相同的实例。 - 如何取消?