473,509 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Remove an Event Handler without knowing the name of the handler

Is there a way to dynamically remove an event handler from an event without
knowing the name of the handler?

For example, how can ClassB remove the handler without knowing the name, or
how many handlers there are?

<code>
Public Class ClassA

Private m_ClassB As New ClassB

Public Sub Add()

m_ClassB.AddHandler(MyHandler)

' ...

m_ClassB.RemoveHandlers()

End Sub

Public Sub MyHandler()
' Handle event
End Sub

End Class

Public Class ClassB

Public Event DoStuff()

Public Sub AddHandler(handler As EventHandler)

AddHandler DoStuff, AddressOf handler

End Sub

Public Sub RemoveHandlers()

' *** Remove unknown handler(s) ***
RemoveHandler DoStuff, ???

End Sub

End Class
</code>

Presumably, the list of currently connected event handlers is held
somewhere. Can it be accessed in some way to remove an indeterminate number
of handlers?

TIA

Charles
Nov 20 '05 #1
4 8696
* "Charles Law" <bl***@nowhere.com> scripsit:
Is there a way to dynamically remove an event handler from an event without
knowing the name of the handler?

For example, how can ClassB remove the handler without knowing the name, or [...]
Public Class ClassB

Public Event DoStuff()

Public Sub AddHandler(handler As EventHandler)

AddHandler DoStuff, AddressOf handler

End Sub

Public Sub RemoveHandlers()

' *** Remove unknown handler(s) ***
RemoveHandler DoStuff, ???

End Sub

End Class
</code>

Presumably, the list of currently connected event handlers is held
somewhere. Can it be accessed in some way to remove an indeterminate number
of handlers?


In 'ClassB', have a look at 'DoStuffEvent'. This is a hidden variable.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Thanks once again Herfried.

I was just never going to find that. Why is it hidden? I have looked it up
in the MSDN and the explanation is pure gobbledegook.

In my example, to remove all handlers of my event in all classes, would I
write

Delegate.RemoveAll(DoStuff, DoStuff) ?

I don't understand why there are two parameters.

Charles
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2i************@uni-berlin.de...
* "Charles Law" <bl***@nowhere.com> scripsit:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler?

For example, how can ClassB remove the handler without knowing the name, or [...] Public Class ClassB

Public Event DoStuff()

Public Sub AddHandler(handler As EventHandler)

AddHandler DoStuff, AddressOf handler

End Sub

Public Sub RemoveHandlers()

' *** Remove unknown handler(s) ***
RemoveHandler DoStuff, ???

End Sub

End Class
</code>

Presumably, the list of currently connected event handlers is held
somewhere. Can it be accessed in some way to remove an indeterminate number of handlers?


In 'ClassB', have a look at 'DoStuffEvent'. This is a hidden variable.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3
Herfried

On the same note, how about the reverse process? That is, given

Public Class ClassA

Public Event MyEvent()
....
AddHandler MyEvent, AddressOf ClassBInstance.MyEventHandler
....
End Class

When ClassBInstance is destroyed, how can all events that MyEventHandler
handles be disconnected without ClassB knowing what events have been linked?
Is there another hidden variable?

Charles
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2i************@uni-berlin.de...
* "Charles Law" <bl***@nowhere.com> scripsit:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler?

For example, how can ClassB remove the handler without knowing the name, or [...] Public Class ClassB

Public Event DoStuff()

Public Sub AddHandler(handler As EventHandler)

AddHandler DoStuff, AddressOf handler

End Sub

Public Sub RemoveHandlers()

' *** Remove unknown handler(s) ***
RemoveHandler DoStuff, ???

End Sub

End Class
</code>

Presumably, the list of currently connected event handlers is held
somewhere. Can it be accessed in some way to remove an indeterminate number of handlers?


In 'ClassB', have a look at 'DoStuffEvent'. This is a hidden variable.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #4
* "Charles Law" <bl***@nowhere.com> scripsit:
I was just never going to find that. Why is it hidden? I have looked it up
in the MSDN and the explanation is pure gobbledegook.
That's VB.NET :-).
In my example, to remove all handlers of my event in all classes, would I
write

Delegate.RemoveAll(DoStuff, DoStuff) ?

I don't understand why there are two parameters.


I currently cannot test it, but try '[Delegate].RemoveAll(FooEvent,
FooEvent)'. You can imagine 'FooEvent' as a "list" of delegates that
are invoked when you do a 'RaiseEvent'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
26016
by: Sujan | last post by:
Hello all, Is it possible to remove scrollbar(s) without using frames. Is there any code which could be applied in body tag to remove scrollbar(s). Thanks in adv, Sujan
1
2308
by: Rob Tiemens | last post by:
hello, I need to select the first and the second column of a table. But I don't know the names of the columns. Is there any way I can select only the first column from a table without knowing...
24
3217
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
2
1785
by: AdamM | last post by:
Currently, my code reads from a database without knowing the order of the columns like this: string name = (string)datarow; However, is there a way to write or do an INSERT back to a database...
0
3270
by: CodeMotion | last post by:
I have access to an internet directory and files will be placed in this directory periodically. I am writing a service to poll that directory and download the files for processing. I have figured...
3
1278
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
3
1757
by: Familjen Karlsson | last post by:
Here is an example from the help on the keword OleDbConnection, in VB.Net, they don't give the path to the database just the word localhost. How can it connect to the database without knowing where...
0
2842
by: Andrzej Lipski | last post by:
Hello I am developing a Windows Ce 5.0 mobile application. I followed the example shown at : Creating Self-Updating Applications With the .NET Compact Framework...
3
1098
by: optimistx | last post by:
I try to develope a bookmarklet, which hopefully can be activated on almost any http-page (i.e. from any server). The page can have unknown event handlers. The bookmarklet wants to add its own...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
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...
1
7067
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...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
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...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
440
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...

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.