Counting the number of Event handlers

A

Amit

Hello!
Is it possible to find how many event handlers an event has at runtime? How
about finding whether or not an event has event handlers? In C# you can
compare the event with null to check if it has event handlers, but I can't
find the equivalent in VB.
Thanks
Amit
 
H

Herfried K. Wagner [MVP]

Amit said:
Is it possible to find how many event handlers an event has at runtime?
How
about finding whether or not an event has event handlers? In C# you can
compare the event with null to check if it has event handlers, but I can't
find the equivalent in VB.

In VB.NET you don't need to compare to 'null', simply use 'RaiseEvent' to
raise the event.
 
A

Amit

Herfried K. Wagner said:
In VB.NET you don't need to compare to 'null', simply use 'RaiseEvent' to
raise the event.

That is fine, but is there a way to find out if there are event handlers?
For example if there's something I need to do only if there are event
handlers before I raise the event..
Thanks
Amit
 
A

Amit

Herfried K. Wagner said:
handlers?

I don't think this sample code is syntactically correct. It has this
snippet:
If FooEvent Is Nothing Then
Return 0
Else
Return FooEvent.GetInvocationList().Length
End If

but FooEvent is never declared. Perhaps the author meant to say Foo, which
is declared as an event but in that case I will not be able to call
GetInvocationList method because it is not static.
Thanks
Amit
 
J

Jay B. Harlow [MVP - Outlook]

Amit,
| but FooEvent is never declared. Perhaps the author meant to say Foo, which
| is declared as an event but in that case I will not be able to call
| GetInvocationList method because it is not static.
Ah! There's the rub!

FooEvent is declared by the compiler, when the compiler sees the Event Foo
declaration.

In other words, its an implementation detail of how the Event keyword is
implemented.

You can use ILDASM to see the defination of FooEvent.

Try it & see, the code will work as demonstrated.

Hope this helps
Jay

|
| | > > That is fine, but is there a way to find out if there are event
| handlers?
| >
| >
|
<URL:http://dotnet.mvps.org/dotnet/code/codingtechnique/#EventHandlerCount>
| >
|
| I don't think this sample code is syntactically correct. It has this
| snippet:
| If FooEvent Is Nothing Then
| Return 0
| Else
| Return FooEvent.GetInvocationList().Length
| End If
|
| but FooEvent is never declared. Perhaps the author meant to say Foo, which
| is declared as an event but in that case I will not be able to call
| GetInvocationList method because it is not static.
| Thanks
| Amit
|
|
 

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