Results 1 to 4 of 4

Thread: EvenHandlers -= question. Delegates are reference based?

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    EvenHandlers -= question. Delegates are reference based?

    Hi,

    For once and for all I would like to clear up something for myself because I have been wondering about this for a long time.

    I have seen several people add eventhandlers in the following way, and so do I:

    Code:
    Something instance = new Something();
    instance.event += new EventHandler(somefunction);
    now let's say you wish to replace the instance at a later point in your application. You would want the eventhandler to be linked with the new instance. Besides that you would want to remove the link with the original instance to avoid memoryleaks. In code samples I have often seen the following:

    Code:
    instance.event -= new EventHandler(somefunction);
    instance = new Something();
    instance.event += new EventHandler(somefunction);
    When I first saw it I had a lot of criticism. This will be ok if EventHandler is a struct, but it will be a mistake if EventHandler is a class. Because in that case the 2nd new EventHandler(...) and the 1st new EventHandler(...) represent different instances so you would be removing nothing at all. However EventHandler is not a class and it is not a structure, it is a delegate.

    So my main question is: is a delegate a reference based thing or is it placed immediatly on the stack like a structure? If it is a reference than I believe the above should be handled as followed to be correct:

    Code:
    //the first time storing the handler in a variable
    Something instance = new Something();
    EventHandler tmpHandler = new EventHandler(somefunction);
    instance.event += tmpHandler;
    
    //so that later the same variable can be removed
    instance.event -= tmpHandler;
    instance = new Something();
    instance.event += new EventHandler(somefunction);
    Should I use this? Or is the simple event -= new Eventhandler(...) ok?

    I hope somebody who had the same question and found some proof to back up one of the 2 theories can share his discoveries with me.

    Thank you
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: EvenHandlers -= question. Delegates are reference based?

    Delegates are reference type objects, like like classes. With that in mind, you would think that your second code snippet should not be effective. In fact it is because event handlers are treated in a special way. It is impractical to expect people to code like your third snippet. When you do this:
    C# Code:
    1. instance.event -= new EventHandler(somefunction);
    you might expect that C# tries to remove that specific event handler from the event. What actually happens is that C# checks all the event handlers assigned to that event and removes the first one it finds that refers to the same method. If none are found then none are removed. If multiple exist then only the first is removed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: EvenHandlers -= question. Delegates are reference based?

    Thank you very much for clearing this up .
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  4. #4

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: EvenHandlers -= question. Delegates are reference based?

    I would love to Add some to your reputation but vbforum tells me I should spread some reputation around before giving it to you again :-D lol.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width