Counting the number of Event handlers

  • Thread starter Thread starter Amit
  • Start date Start date
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
 
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.
 
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
 
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
 
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
|
|
 
Back
Top