Of course it is... You create an instance of (I assume its A even though you wrote B), and then set up the event handler, and let A go out of scope, which disposes it. If you want to hold references and have your events work, class B should look more like this:
public class B
{
A myInstanceOfA;
public B()
{
myInstanceOfA = new A();
MyInstanceOfA.MyEvent+=MyEvent_EventHandler;
}
}
Since the reference is kept alive for the life of B, so will the event handler.
[Edit]
In response to your additional information, here is a test I made:
class Program
{
static void Main(string[] args)
{
B myB = new B();
myB.MyA.RaiseEvent();
Console.ReadKey();
}
}
class A
{
public event EventHandler MyEvent;
public void RaiseEvent()
{
Console.WriteLine("Raising Event");
Console.WriteLine("Event handler is " + (MyEvent == null ? "null" : "set"));
if (MyEvent != null)
MyEvent(this, EventArgs.Empty);
}
}
class B : INotifyPropertyChanged
{
private A _myA;
public A MyA
{
get { return _myA; }
set
{
if (_myA != value)
{
_myA = value;
OnPropertyChanged("MyA");
}
}
}
public B()
{
MyA = new A();
MyA.MyEvent += MyA_MyEvent;
}
void MyA_MyEvent(object sender, EventArgs e)
{
Console.WriteLine("Got event from A");
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
And the output is:
Raising Event
Event handler is set
Got event from A
So I'm not sure what you are doing wrong. You can copy/paste the above into a console application and see it work.