Question about custom events

Posted by Malfist on Stack Overflow See other posts from Stack Overflow or by Malfist
Published on 2010-05-07T06:47:25Z Indexed on 2010/05/07 6:58 UTC
Read the original article Hit count: 150

Filed under:
|
|

I'm making custom events for C# and sometimes it isn't working.

This is how I'm making the event happen:

    private bool isDoorOpen;
    public bool IsDoorOpen {
        get { return isDoorOpen;}
        private set { isDoorOpen = value; DoorsChangeState(this, null);}
    }

And these are the event declarations:

    //events        
    public delegate void ChangedEventHandler(Elevator sender, EventArgs e);
    public event ChangedEventHandler PositionChanged;
    public event ChangedEventHandler DirectionChanged;
    public event ChangedEventHandler BreaksChangeState;
    public event ChangedEventHandler DoorsChangeState;

This works as long as there are methods attached to the events, but if there isn't, it throws a null ref exception. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET