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

C

Charles Law

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
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
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.
 
C

Charles Law

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 said:
* "Charles Law said:
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.
 
C

Charles Law

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 said:
* "Charles Law said:
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.
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top