470,615 Members | 2,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,615 developers. It's quick & easy.

Removing events

I have the following code, which appears to be working. However it doesn't
look right. The part I am wondering about is the logic in
DisconnectEvents(). This logic creates a new delegate and then expects to
find that delegate in the list of event handlers.

What is the proper way of writing this?

Regards,
Bob Rundle
public void ConnectEvents(joaCollectionEvents events)
{
OnChanged += new OnChangedDelegate(events.OnChanged);
OnAdded += new OnAddedDelegate(events.OnAdded);
OnRenamed += new OnRenamedDelegate(events.OnRenamed);
OnRemoved += new OnRemovedDelegate(events.OnRemoved);
}

public void DisconnectEvents(joaCollectionEvents events)
{
OnChanged -= new OnChangedDelegate(events.OnChanged);
OnAdded -= new OnAddedDelegate(events.OnAdded);
OnRenamed -= new OnRenamedDelegate(events.OnRenamed);
OnRemoved -= new OnRemovedDelegate(events.OnRemoved);
}
Nov 16 '05 #1
2 1707
Bob Rundle <ru****@rundle.com> wrote:
I have the following code, which appears to be working. However it doesn't
look right. The part I am wondering about is the logic in
DisconnectEvents(). This logic creates a new delegate and then expects to
find that delegate in the list of event handlers.

What is the proper way of writing this?


That's fine. So long as it's got the same target as the original
delegate, and the same method, it'll match.

(If you'd changed the value of "events", for example, nothing would
have been removed.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Bob,
You could also keep a reference to the added delegates:

OnChangedDelegate ocHandler = null;

public void ConnectEvents(joaCollectionEvents events)
{
ocHandler = new OnChangedDelegate(events.OnChanged);
OnChanged += ocHandler;
}

public void DisconnectEvents(joaCollectionEvents events)
{
if ( ocHandler!= null )
OnChanged -= ocHandler;
}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Bob Rundle" <ru****@rundle.com> wrote in message
news:u%***************@TK2MSFTNGP12.phx.gbl...
I have the following code, which appears to be working. However it doesn't look right. The part I am wondering about is the logic in
DisconnectEvents(). This logic creates a new delegate and then expects to
find that delegate in the list of event handlers.

What is the proper way of writing this?

Regards,
Bob Rundle
public void ConnectEvents(joaCollectionEvents events)
{
OnChanged += new OnChangedDelegate(events.OnChanged);
OnAdded += new OnAddedDelegate(events.OnAdded);
OnRenamed += new OnRenamedDelegate(events.OnRenamed);
OnRemoved += new OnRemovedDelegate(events.OnRemoved);
}

public void DisconnectEvents(joaCollectionEvents events)
{
OnChanged -= new OnChangedDelegate(events.OnChanged);
OnAdded -= new OnAddedDelegate(events.OnAdded);
OnRenamed -= new OnRenamedDelegate(events.OnRenamed);
OnRemoved -= new OnRemovedDelegate(events.OnRemoved);
}

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by m0nkeymafia | last post: by
3 posts views Thread by MikeP | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.