C
Christopher D. Wiederspan
This is a bit tough to figure out the best way to ask, but here it goes.
I've got a class, say MyObject, and everytime this class is instanciated, I
actually want to get a reference to an existing object. Maybe better stated
by example:
public class SomeClassA {
MySpecialObject A;
public SomeClassA() {
this.A = new MySpecialObject();
A.FireSpecialEvent();
}
}
public class SomeClassB {
MySpecialObject B;
public SomeClassB() {
this.B = new MySpecialObject();
this.B.SpecialEvent += new System.EventHandler(this.B_SpecialEvent);
}
private void B_SpecialEvent(object source, System.EventArgs e) {
// Do something here...
}
}
So, the trick is, that I want both MyObject's A and B to reference the same
object, so that when the line "A.FireSpecialEvent()" is called, it will
trigger the event handler in the SomeClassB object (assuming that the
constructor for SomeClassB has already been called). My problem up to this
point is that since A and B are actually referencing different objects, the
events don't fire across (of course).
Any ideas would be greatly appreciated. I'm hoping there's a way to maybe
override the 'new' method or something so that both A and B will end up
referencing the same object instance...? Or maybe there's a whole better way
to do this with some sort of global Event mechanism that I don't know about.
Thanks,
Chris
I've got a class, say MyObject, and everytime this class is instanciated, I
actually want to get a reference to an existing object. Maybe better stated
by example:
public class SomeClassA {
MySpecialObject A;
public SomeClassA() {
this.A = new MySpecialObject();
A.FireSpecialEvent();
}
}
public class SomeClassB {
MySpecialObject B;
public SomeClassB() {
this.B = new MySpecialObject();
this.B.SpecialEvent += new System.EventHandler(this.B_SpecialEvent);
}
private void B_SpecialEvent(object source, System.EventArgs e) {
// Do something here...
}
}
So, the trick is, that I want both MyObject's A and B to reference the same
object, so that when the line "A.FireSpecialEvent()" is called, it will
trigger the event handler in the SomeClassB object (assuming that the
constructor for SomeClassB has already been called). My problem up to this
point is that since A and B are actually referencing different objects, the
events don't fire across (of course).
Any ideas would be greatly appreciated. I'm hoping there's a way to maybe
override the 'new' method or something so that both A and B will end up
referencing the same object instance...? Or maybe there's a whole better way
to do this with some sort of global Event mechanism that I don't know about.
Thanks,
Chris