469,646 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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 8317
* "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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by optimistx | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.