473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1828
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 =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.