I have created an external class (TheClass
) with an event (TheEvent
) and subscribe to it from within another class's Panel constructor:
public aPanel()
{
...
theClassInstance.TheEvent += new WaitCallback(aMethod);
...
}
Later in the program, I call a method passing theClassInstance
as the only parameter
bMethod((object)theClassInstance);
where
public void bMethod(object inputTheClassInstance)
{
...
}
Knowing that the input object is of type TheClass
, I do the following:
public void bMethod(object inputTheClassInstace)
{
TheClass theClassInput = (TheClass)inputTheClassInstace;
...
}
Later in bMethod()
I call a method RaiseEvent()
exposed by theClassInput
which will actually trigger the event. In RaiseEvent()
I have
if(this.TheEvent != null)
TheEvent();
to make sure something is subscribed to the event but this.TheEvent
equates to null. If I place the subscription within bMethod()
bMethod(...)
{
...
theClassInput.TheEvent += new WaitCallback(aMethod);
...
}
it works just fine but I would like to keep it in the Panel's constructor. I figured that because theClassInput
is pointing to the same object as theClassInstance
it wouldn't make a difference which triggers the event. Any thoughts on how I can keep the subscription in the constructor while calling it from within bMethod()
using theClassInput
?
Passing an object around does not clear event handlers. You must accidentally create a new object somewhere along the way instead of passing the original one.