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

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

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


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

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

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

P: n/a
* "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 discussion thread is closed

Replies have been disabled for this discussion.