Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3597744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:09:25+00:00 2026-05-18T20:09:25+00:00

I have a ugly piece of code that adds event handlers. The problem is,

  • 0

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
    //
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:09:25+00:00Added an answer on May 18, 2026 at 8:09 pm

    SLaks is correct, and has linked to some excellent resources. Here’s a relevant quote from Chris Burrows’ blog article:

    Let me take a quick detour here and explain to you how the binding of += works in C#. There are two possibilities:

    1. either there is an actual + operator, such as with ints, and x += y binds to “x = x + y” except that x is only evaluated once. This is the compound assignment operator; or
    2. the thing on the left hand side is an event, and x.E += y binds to “x.add_E(y)”. This is the event accessor operator, and in fact this is the only way to bind to an event accessor.

    So what do we have in the snippet above? Well, the extra bit of detail that you need to decide is the following rule about field-like events in C#: outside of the class or struct that defines a field-like event E, binding to the name E resolves to the event itself, on which the only legal operation is calling an accessor; inside the class or struct that defines a field-like event E, binding to the name E resolves to the private delegate field.

    In your case, when resolving myControl.Event, you’re inside the myControl class, so you don’t see an event object; instead you see an actual delegate object, which you can compare with null. When resolving myControl.TreeView.NodeMouseClick, you’re outside the TreeView class, so you can’t access the actual delegate object; all you get is the event object, which cannot be compared to null.

    If I understand correctly, all of this wouldn’t help you anyway, since presumably after you check for null, you’re going to try to fire the TreeView‘s event for it, which you can’t do.

    Depending on what you’re trying to do, you could probably subclass TreeView and add an internal method that would call the protected TreeView.OnNodeMouseClick method to fire the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ugly piece of code that adds event handlers. The problem is,
I have a particularly ugly piece of test code that is calling a number
So I have made a little piece of code that will insert and manipulate
I have an ugly piece of Serial Port code which is very unstable. void
I have this piece of code that searches for a folder given a starting
I have a JavaScript widget (a piece of embedded JS and HTML code) that's
I have the following ugly if statement that is part of a class that
I have some C# code that walks XML schemata using the Xml.Schema classes from
I have written a small piece of code which handles the input of a
I have a legacy piece of software that has a single manager which controls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.