How to share a variable between two classes?
Posted
by Altefquatre
on Stack Overflow
See other posts from Stack Overflow
or by Altefquatre
Published on 2010-03-29T04:41:10Z
Indexed on
2010/03/29
4:53 UTC
Read the original article
Hit count: 271
How would you share the same object between two other objects? For instance, I'd like something in that flavor:
class A
{
private string foo_; // It could be any other class/struct too (Vector3, Matrix...)
public A (string shared)
{
this.foo_ = shared;
}
public void Bar()
{
this.foo_ = "changed";
}
}
...
// inside main
string str = "test";
A a = new A(str);
Console.WriteLine(str); // "test"
a.Bar();
Console.WriteLine(str); // I get "test" instead of "changed"... :(
I read there is some ref/out stuff, but I couldn't get what I'm asking here. I could only apply some changes in the methods scope where I was using ref/out arguments... I also read we could use pointers, but is there no other way to do it?
© Stack Overflow or respective owner