472,145 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

What is happening with -= and Event delegates ?

Hi,

What is required to unwire an event ? I wire it with:
this.MyEvent += new MyEventHandler()

Do I need to maintain a handle to the eventhandler created so that I can
detach it ?

The following seems to work, but it doesn't feel right as I seem to be
adding one object, but then removing another:
this.MyEvent -= new MyEventHandler()

What is happening here ?

Thanks

Barry Mossman
Nov 16 '05 #1
2 1273
delegates are not reference based (or at least not stored that way), so
removing a delegate from a delegatelist is as easy as recreating the handler
and removing it from the list using the -= syntax. What I mean by this is
the following:

The Delegate.Equals method which is used to determine if a delegate in the
list is equal to the delegate that you are attempting to remove compare the
method pointers, not the object references. This means that if they are
pointing to the same method then they are equal.

You are doing it correctly.

"Barry Mossman" <Ba**********@hotmail.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
Hi,

What is required to unwire an event ? I wire it with:
this.MyEvent += new MyEventHandler()

Do I need to maintain a handle to the eventhandler created so that I can
detach it ?

The following seems to work, but it doesn't feel right as I seem to be
adding one object, but then removing another:
this.MyEvent -= new MyEventHandler()

What is happening here ?

Thanks

Barry Mossman

Nov 16 '05 #2
"Dave" <NO*********@dotcomdatasolutions.com> wrote in message
news:uE*************@TK2MSFTNGP12.phx.gbl...
You are doing it correctly.


Great. Thanks Dave.
and your snipped explanation helped a lot also .

Barry Mossman
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andy Read | last post: by
5 posts views Thread by Ian Richardson | last post: by
2 posts views Thread by Lore Leuneog | last post: by
2 posts views Thread by Lore Leuneog | last post: by
reply views Thread by mickeymicks | last post: by
1 post views Thread by Phil Townsend | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.