472,976 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1809
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TP-Software | last post by:
Hi, I have a ChatFormCollection class which holds a collection of the class ChatForm which inherits from Form. MainForm has an instance of ChatFormCollection. When a ChatForm instance is...
2
by: Martin Lapierre | last post by:
How can I remove the default exception handler handler? When adding a custom handler, I can't get rid of the VS.NET unhandled exception dialog. Ex: AppDomain currentDomain =...
5
by: Patrick | last post by:
Hello I have the following problem. I have an aspx Page. This page contains an ASP-Table Object. So in that table, I have a linkbutton. So, I can't edit the event of that button trough the...
5
by: Charles Law | last post by:
I think I asked the wrong question last time, so I am starting a separate post to distinguish them. Take five classes: ClassA and ClassW...Z ClassA raises five events: Event1...5 ClassW...Z...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
5
by: gnassar | last post by:
Essentially my problem is that .NET 2005 is removing my event handlers. There's no real special things about my project, it just continually removes them all. It starts on the open of a...
4
by: m0nkeymafia | last post by:
I read about this technique to basically allow you to have a file with behaviours and javascript events that attach to various dom objects and events but are not done inline. so its basically a...
3
by: MikeP | last post by:
Hi there, I've tried hunting for this in the C# specs but can't find the relevant info. Does anyone know what the official rule says (or doesn't say) about removing an event more than once. If...
6
by: bgold12 | last post by:
Hey, I just want to make sure that when I remove an element I don't have to worry about the events listeners I added previously to the element. For example: // get the element by its id elem =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.