Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a class - say class A - with a simple event handler declared in it, and a void to fire that event:

public class A
{
    public event EventHandler MyEvent;

    private void FireEvent()
    {
        if (MyEvent != null)
            MyEvent(this,EventArgs.Empty);
    }
}


Another class subscribes to that event within it's constructor:

public class B
{
    public B()
    {
        B myInstanceOfB=new B();
        MyInstanceOfB.MyEvent+=MyEvent_EventHandler;
    }
}


The problem is that, when I call MyEvent within class A, class B doesn't react and MyEvent shows null. Any idea why and how it might be fixed? Thanks in advance to anyone who comes up with an answer!
Posted

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:

C#
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:

C#
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.
 
Share this answer
 
v3
Ron, thanks for the solution! I really missed the issue of scope - must be brainstorming or something! I changed it like that:

public class B
{
    public B()
    {
        MyInstanceOfA = new A();
        MyInstanceOfA.MyEvent+=MyEvent_EventHandler;
    }

    private A _MyInstanceOfA;

    public A MyInstanceOfA
    {
        get{return _MyInstanceOfA;}
        set
        {
            if (_MyInstanceOfA!=value)
            {
                _MyInstanceOfA=value;
                OnPropertyChanged("MyInstanceOfA");
            }
        }
    }
}


...where, of course, you should recognize the well known pattern of property changed notification (since that instance has to be binded).But again...it doesn't work! :(


Update: The darned instance was instanciated somewhere else!!!!!! I must be really stupid to have missed that!!!!!!!!
 
Share this answer
 
v2
Comments
Ron Beyer 26-Jan-14 1:09am    
See my updated solution, its a 100% working sample so I'm not sure what you are doing to test if its working or not, you can copy/paste the code into a console application to see it work.
Geysser 26-Jan-14 1:12am    
It's OK! Thank's for your effort! I solved it myself - and it was, really, a stupid mistake on my behalf! Thank's anyways!!! :)
Kiran_Reddy 27-Aug-16 17:41pm    
Hi, i have similar issue, how did you solve it? I am working on it from yesterday.
Please let me know asap. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900