Check if object handles/is hooked up to a certain type of event/event handler

N

nford

Is there a way to check if an object handles a certain type of event or
if the object is hooked up to a certain event handler/call back
function?

For instance what would be the equivalent syntax in the following:

Private x as Object
Public Event TestEvent()

Sub Y()
if (x handles TestEvent) then Write("X Handles TestEvent()")
End Sub

-----------------------------------------------------------------

Private x as Object
Public Event TestEvent()

Sub New()
AddHandler x, AddressOf XHandlerFunction
End Sub

Sub XHandlerFunction()
' handling code
End Sub

Sub CheckHookup()
' Consider multiple handlers
If x.handlers(0) Is XHandlerFunction Then
Write("X has XHandlerFunction() as one of its handlers")
End If
End Sub
 
L

Larry Lard

Is there a way to check if an object handles a certain type of event or
if the object is hooked up to a certain event handler/call back
function?

Yes. When you write

Public Event TestEvent()

then behind the scenes two things get created: a delegate class named
TestEventEventHandler, and an instance of this class, named
TestEventEvent. Then, TestEventEvent.GetInvocationList is an array of
Delegate, which contains all the handlers hooked to that event. There
is no difference in the treatment of an event with one handler and one
with many handlers.

To examine the invocation list:

Dim d as TestEventEventHandler
for each d in TestEventEvent
if d Is X Then
'... X is hooked to this event
end if
next
 

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