By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,660 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,660 IT Pros & Developers. It's quick & easy.

Removing events

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.