Hi,
Supose that:
object x;
void GiveMeARef(ref object obj)
{
x = obj;
}
So I want that X change this value when Y is changed. For instance:
Y = new MyObject;
GiveMeARef(Y); //So x == Y;
Y = new OtherObject; // I want that X continues equals to Y, but it doesn't:\
Anyone knows how I can do this in C# if it is possible?
Sorry about my poor english
Thanks in advance
-
This code smells of a larger problem ... but why not just do:
var y = new MyObject(); GiveMeARef(ref y); var z = new MyObject();
No need to reuse y.
-
without getting into low-level debugging techniques (like what Visual Studio uses when it's debugging your code), I don't think there's a straightforward way to do this.
What you're looking for is an alias, not a reference, and to my knowledge, that isn't supported by the CLR.
You might be able to do it by writing
unsafe
code, but I would suspect that there might be some significant side-effects and complications from doing this.Kevin : I agree. It looks like he's trying to mimic the behavior of a C++ alias, not anything that normally happens in the CLR. Just by the OPs method `GiveMeARef` it shows a mis-understanding of references in C# IMO. -
It seems like you want
X
to be a pointer toY
. Pointers aren't supported directly in C# (only in unsafe code) but you can achieve this properly by a litte hack with closures/lambda-expressions.Try converting this VB-tip to C# (
Pointer<T>-Structure
) -
There is no way to do what you are attempting (directly).
The closest thing I can think of would be to have Y be encapsulated inside of some other class, and have GiveMeARef() take the class instance instead of Y directly. This would look something like:
myClass.Y = new MyObject(); GiveMeARef(ref myClass); myClass.Y = new MyObject();
Then you'd be able to still have the internal reference find Y - even though it wasn't staying exactly the same.
-
The easiest solution is to assign values to Y using a property, and update X in the setter function:
private MyObject m_Y = new MyObject(); public MyObject Y { get { return m_Y; } set { m_Y = value; X = value; // Here ... } }
Another solution is creating a new class, MyObjectWrapper, and deal with it instead ...
Another thing, there isn't any point of the 'ref' in GiveMeARef(ref object obj) .. it's already passed by reference.
-
It really isn't clear to me what you are trying to do, but I'd be using a property:
private SomeType y; public SomeType Y { get { return y;} set { if(y!=value) { y = value; // run any "things to do when Y changes" code OnYChanged(); } } } public event EventHandler YChanged; protected virtual void OnYChanged() { EventHandler handler = YChanged; if(handler!=null) handler(this, EventArgs.Empty); }
Now you can watch for changes to
obj.Y
in 3 different ways:- in the
Y
setter - via the
YChanged
event - by subclassing and overriding
OnYChanged
- in the
-
This would also achieve the result, keeping the assignment at the time of the "new" operation, assuming that in your example you are using many different news and want X to be updated each time.
private static object x; private static object CreateNew(Type t) { object temp; temp = System.Activator.CreateInstance(t); x = temp; return temp; } static void Main(string[] args) { object o = CreateNew(typeof(int)); o = CreateNew(typeof(long)); }
Where you use reflection to create the instance of the object and assign it to the keeper variable at the same time. It very much depends on what the nature of the problem is as to which is the appropriate solution.
0 comments:
Post a Comment