need pointers?

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
 
J

Jon Skeet [C# MVP]

lukasz said:
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?

Well, the solution is to avoid the design which requires that. Changing
the value of one local variable to another *shouldn't* (IMO) start
changing the values of objects within the system.
 
N

Nick Malik

The solution is: Learn Object Oriented programming. Start with the
literature on Design Patterns.

Any design that requires pointers is flawed. Fix the design and the
uncomfortable workaround would not be necessary.

If you want to share details on what it is you are trying to achieve, rather
than just giving us the technical tactics, we may be able to help you solve
the problem in a far more object-oriented way.

--- Nick
 
S

Stoitcho Goutsev \(100\) [C# MVP]

lukasz,

No, it is not possible. The same is not possible even in c++ using pointers.
The latter is not completely correct, though you maight be able to do
something with pointer-to-pointer or reference, but if you do that with
local variables (it seems this would be a common case) it would be dangerous
due to different lifetimes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top