Behind the scenes of events in VB.NET#

Take the following event declaration (defining the parameter list immediately) in VB.NET:

Public Event SomethingHappened(ByVal sender As Object, ByVal e As SomethingHappenedEventArgs)

The VB.NET compiler is 'kind enough' to generate an appropriate delegate type for us behind the scenes - in this case:

Public Delegate Sub SomethingHappenedEventHandler(ByVal sender As Object, ByVal e As SomethingHappenedEventArgs)

Note that you can also define the delegate type yourself and declare the event more in a C# type of way:

Public Delegate Sub SomethingHappenedEventHandler(ByVal sender As Object, ByVal e As SomethingHappenedEventArgs)
Public Event SomethingHappened As SomethingHappenedEventHandler

This approach is better because otherwise, a new delegate type will always be created by the compiler - even if you declare different events with an identical signature which could and should use the same delegate type. By defining the delegates yourself you have more control over what is created.

Ok so now I'm trying to determine if there are any subscribers to my event. In C# I could get away with a simple "if( SomethingHappened != null )" but in VB.NET SomethingHappened is just the event declaration, not an actual object, so I can't use "If Not SomethingHappened Is Nothing". The generated (or declared) delegate is also just a type, not an object, so I can't just go "If SomethingHappenedEventHandler.GetInvocationList().Length > 0". I spent some time surfing the web looking for an answer, and after a while I found out I should've looked at the documentation straight away. As it happens, the compiler also generates a private member field called SomethingHappenedEvent which holds the actual delegate instance used to maintain the event's invocation list. So the solution was to use "If SomethingHappenedEvent.GetInvocationList().Length > 0".

So when you declare an event as above, you actually get this:

Public Delegate Sub SomethingHappenedEventHandler(ByVal sender As Object, ByVal e As SomethingHappenedEventArgs)
Private SomethingHappenedEvent As SomethingHappenedEventHandler

According to the documentation, the compiler also generates some methods to add and remove event handlers but I didn't find them. Note that all these compiler-generated types and fields aren't visible in IntelliSense so they're not obvious to discover.

I must say I'm a lot more comfortable in C# where there are no auto-generated features of type "let's-make-the-basics-easy-but-cause-confusion-for-people-that-actually-know-what-delegates-are". Delegates and Events are the core of event-driven programming in .NET and should be well understood when programming them. This is no playground for compilers, this is a must-know for developers. Another boo to VB.NET!

If you want to know about how events actually work, go read the Bedtime Story, really!

Thursday, May 5, 2005 3:10:12 PM (Romance Daylight Time, UTC+02:00)
At last! I have searching for this all day (in order to use it in remoting events).
Thanks!
Comments are closed.
All content © 2014, Jelle Druyts
On this page
Top Picks
Statistics
Total Posts: 351
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.3.12105.0

Sign In