C # - detection on object reference change

Let's pretend that:

object x;

void GiveMeARef(ref object obj)
{
   x = obj;
}

      

So, I want X to change this value when Y changes. For example:

Y = new MyObject;
GiveMeARef(Y); //So x == Y;
Y = new OtherObject; // I want that X continues equals to Y, but it doesn't:\

      

Does anyone know how I can do this in C # if possible?

Sorry for my bad english

Thanks in advance

+1


a source to share


7 replies


This code smells like a big problem ... but why not just do:

var y = new MyObject();
GiveMeARef(ref y);
var z = new MyObject();

      



No need to reuse y.

+1


a source


without resorting to low-level debugging techniques (like what Visual Studio uses when debugging your code), I don't think there is an easy way to do this.

What you are looking for is an alias, not a link, and as far as I know this is not supported by the CLR.



You may be able to do this by writing code unsafe

, but I suspect there may be some significant side effects and complications from this.

+1


a source


It's really not clear to me what you are trying to do, but I would use the 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 track changes in obj.Y

three ways:

  • in Y

    setter
  • through event YChanged

  • by subclassing and overriding OnYChanged

+1


a source


You seem to want to X

be a pointer to Y

. Pointers are not directly supported in C # (only in unsafe code), but you can achieve this properly with a litte hack with closures / lambda expressions.

Try converting this VB tip to C # ( Pointer<T>-Structure

)

0


a source


There is no way to accomplish what you are trying (directly).

The closest thing I can think of would be for Y to be encapsulated inside some other class, and let GiveMeARef () take an instance of the class instead of Y directly. It will look something like this:

myClass.Y = new MyObject();
GiveMeARef(ref myClass);
myClass.Y = new MyObject();

      

Then you can still have an internal find Y reference - even if it doesn't stay exactly the same.

0


a source


The simplest solution is to assign Y values ​​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 to create a new class MyObjectWrapper and solve it ...

Another thing is that there is no "ref" point in GiveMeARef (ref object obj) .. it is already passed by reference.

0


a source


This will also have the effect of saving the assignment during a "new" operation, assuming that in your example you are using a lot of different news and want X to be updated every 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));


}

      

If you are using reflection to instantiate an object and at the same time assign it to a store variable. It largely depends on what is the nature of the problem, what is the appropriate solution.

0


a source







All Articles