473,574 Members | 5,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1314
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**********@h otmail.com> wrote in message
news:eh******** ******@TK2MSFTN GP12.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*********@do tcomdatasolutio ns.com> wrote in message
news:uE******** *****@TK2MSFTNG P12.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7029
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
5
2494
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another, so it becomes: handle.onreadystatechange = function() { whatever_was_previously_defined(whatever);some_other_function(handle) };
2
1058
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely Greetings Lore
2
3075
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely Greetings Lore
2
1262
by: al | last post by:
Greetings, Can someone please tell me the what is the defference betweeen delegates and events handler??? MTIA, Grawsha
4
3408
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this, and some practical examples where Delegates would be better/worse to use...
9
3176
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will load with different data (locations, departments, etc.).
0
1249
by: mickeymicks | last post by:
hi all! <i'm fairly new to csharp (and dotnet) and is particularly confused with delegates and events.. so forgive me if im a bit dense..> Is there any major concern that i need to think about when triggerring an event or a delegate using DynamicInvoke() versus simply calling the event.. MyEvent.DynamicInvoke(sender, args);
1
4595
by: Phil Townsend | last post by:
I have an application that needs to respond to events that occur outside of the application itself. My project, called "ShowDetection" declares the event. I have a console app called "TestEvent" that I would like to use to test the event handler. Any action in the console app would be acceptable, such as a keystroke. I am at a loss on how to...
0
7741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3781
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.