Testing Events for Nothing/Null (Doug Rothaus)

Published 25 September 09 10:10 AM

While helping some Windows API folks with some sample code this week, I stumbled upon...uh...I mean “carefully researched” an issue that you might find handy.

You may be aware that the RaiseEvent statement automatically checks to verify if an event handler exists before raising the event. If the event is Nothing, then there’s no event handler and RaiseEvent terminates. If the event is not Nothing, then RaiseEvent triggers the event.

However, what if you want to follow a different code path if the event is Nothing? Unfortunately, the RaiseEvent statement doesn’t return a value that could indicate whether the event was fired. So, what’s a developer to do?

The answer? When the VB compiler sets up your event, it creates a private variable for the event in the form of <event name>Event. So, if I set up an event named TriggerMe, the VB compiler creates an event variable named TriggerMeEvent. You can test this variable for Nothing. It’s as simple as that! (Note: You won't see this variable using Intellisense, but you can find it when debugging or through reflection.)

Here’s a quick example:

    Public Event SolutionFound As EventHandler

 

    Private Sub OnSolutionFound()

        If SolutionFoundEvent IsNot Nothing Then

            RaiseEvent SolutionFound(Me, New EventArgs())

        Else

            ' No event handler has been set.

            MsgBox("There is no event handler. That makes me sad.")

        End If

    End Sub

by VBTeam
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Nah said on September 29, 2009 3:55 AM:

Is this really a good idea?

I would expect the behaviour to be exactly the same if there is no event handler as if there is one that ends up doing nothing.

The only "safe" use I can imagine is perhaps skipping some checks after the event that you otherwise would have to do in case something was changed during the event.

# Héctor said on September 30, 2009 6:13 AM:

I was already aware of this, but since the VB compiler already adds the code and checks for you is not like in C# where this is a common practice. So I wonder... when is this actually needed? never found a situation where the event variable is nothing.

# Chance said on October 1, 2009 3:27 PM:

"...it creates a public variable for the event.."

Not really.  It creates a private member in the class.

# VBTeam said on October 1, 2009 4:28 PM:

Changed "public" to "private". Thanks for the catch.

# Waleed El-Badry said on October 5, 2009 2:06 AM:

Thanks Doug,

It was really useful to me.

# DeWolf said on October 15, 2009 3:18 AM:

I might be missing the point here, but why not use a custom event.  A RaiseEvent accessor is used in a custom event to specify the statements to execute when the event is raised using the RaiseEvent statement.   Typically, this invokes a list of delegates maintained by the AddHandler and RemoveHandler accessors.  But this can also be used to check for Nothing.

# help said on October 15, 2009 10:13 AM:

Any idea what to do if you assigned an eventhandler and it is still nothing?

I had four event handlers assigned that work, then added a fifth in the same location that always shows up as Nothing.  The others still work properly and keep their pointers.

(using ASP.NET 3.5 sp1 with VB, by the way)

# Yu Heng Zhou said on November 30, 2009 4:51 PM:

it is really useful for me.

SolutionFoundEvent is a hiden member, haha!

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker