Why can I check some event handlers for null, some not?

Posted by Inno on Stack Overflow See other posts from Stack Overflow or by Inno
Published on 2010-12-21T16:10:23Z Indexed on 2010/12/21 16:54 UTC
Read the original article Hit count: 160

Filed under:
|
|

Hi,

I have a ugly piece of code that adds event handlers. The problem is, if the code is called multiple times, the event handlers are called multiple times.

To solve the problem, I remove the event handler first and then add it.

Now I've seen the following behaviour:

Some event handlers can be checked like:

if (object.event == null) {
    //
    // Code
    //
}

others of the form

if (object.object.event == null) {
    //
    // Code
    //
}

I get a message like 'object.object.event' may only occur left of -= or +=. (Since I'm using a german version of visual studio, I don't know the correct translation to english).

I have no idea why the behaviour looks this inconsequent so I would be grateful for some information on this.

To be more specific: It's user control.

if (myControl.Event == null) {
    //
    // works
    //
}

if (myControl.TreeView.NodeMouseClick == null) {
    //
    // doesn't work
    //
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET