Custom Events in VB.NET 2005

Visual Basic .NET is well known for its event-driven programming capabilities. VB.NET 2005 adds new functionality for custom events that provides flexibility in handling and controlling events.

Creating custom events with Visual Basic .NET 2005

Events model in VB.NET

Events are defined as actions or occurances of interest. An interested object can subscribe to an event and be notified when it happens. For example, a button on a form can generate a click event and the form can subscribe and get notified when the event occurs. This click event is processed through the form code.

VB.NET 2005 provides a range of flexible event models to address simple or very complex events. Below is an example of a simple event using a Worker class with a DoWork method:

The DoWork method has a For loop repeating 100 times, waiting one second between loops, and printing out the completed work to the console. But a client such as a form using the object of the above class, for example, should provide visual feedback on how much work has been completed when the DoWork method is called. Adding an event to the Worker class and altering the DoWork method to call the event can provide this information to the client.

An event called WorkDone has been identified with an integer argument through which the amount of work completed can be passed. And in the DoWork method, the event passing the actual amount of work completed is raised. Now the above class can be consumed by a client by declaring and creating a Worker class object with events and handling the WorkDone event to provide feedback on progress.

In the above code, the work_WorkDone method will be called each time the WorkDone event is raised. The Handles clause after the method signature links the event-handling code with the actual event.

A method can also dynamically link to an event using the AddHandler method and be removed from handling an event using the RemoveHandler method as seen below. This enables more flexibility by relating a method to an event and removing the binding required at runtime. Even when the Handles keyword is used to bind a method to an event internally, a call to the AddHandler method is done to perform the actual binding.

‘We dont require to use WithEvents since we are binding to events dynamically
Private mWorker As New Worker

Delegates in .NET events

VB.NET implements events through a construct called delegates. Delegates point to a method and can hold the address of a method, enabling a method to be invoked through an instance of its delegate. The Worker class can be implemented without events using a delegate as follows:

In the code above, a delegate named WorkDone that accepts an integer argument has been defined. This delegate can point to any method with a similar signature (a sub-procedure that accepts an integer). After the delegate is defined, a public variable delegate named Handler has been defined, and instead of raising the event in the DoWork method, the delegate instance is invoked.

In the Worker class client, the delegate instance is set to point to the method that will be called when reporting the amount of work done.

The above code uses delegates directly, not the event construct, so neither the AddHandler nor RemoveHandler methods can be used. This exposes only one public variable, so there can be only one method to handle the invocation of the delegate.

Directly using the delegates method eliminates the setting of multiple event handlers for one event source, which would notify all handlers when the event occurs. But an event type can be set as a delegate and use the event-handling mechanism, resulting in cleaner code as follows:

Custom events in VB.NET 8.0

Custom events provide flexibility and control in creating and managing events. When using custom events, it is the responsibility of the developer to bind and unbind handlers, as well as to specify the actual calls to raise the events. Although this requires a bit more code than the earlier implementations, it provides more control over the handlers and the invocation.

A custom event is defined by using the Custom keyword in front of the event declaration, and by providing the delegate type for the event. The custom event has three sections: The AddHandler section, which is invoked whenever a handler is added using the Handles keyword or the AddHandler method; RemoveHandler section, which is called whenever a handler is removed using the RemoveHandler method; and the RaiseEvent section that will be invoked whenever an event is requested. The developer should write the necessary code in each of these sections to manage the handlers, as well as specify how handlers will be notified when an event is requested.

Below, the Worker class is implemented using the custom event construct in VB.NET 2005.

Using custom events enables more control over the handlers. In the above code, for example, there is a condition in the AddHandler section limiting the number of handlers for which events are provided. The RaiseEvent method has been changed to notify the handlers only when the completed Work is more than 50. Another advantage to using custom events is a central unit of code containing all of the handlers of the event within the event construct.

Using RaiseEvent twice, once in the DoWork method and then in the custom event construct, can lead to confusion. RaiseEvent in the DoWork method will raise the event, but the RaiseEvent section in the custom event construct specifies the code to be executed when the event is raised. In this case, all of the handlers are looped through and delegates are invoked.

Events and delegates are important parts of the .NET language. VB.NET 2005 gives developers control and flexibility in handling simple event models and custom events.

For more articles like this, sign up to the fortnightly Simple-Talk newsletter.

Tags: , , , , , , ,

  • 148162 views

  • Rate
    [Total: 178    Average: 3.8/5]
  • Anonymous

    Good stuff
    Really helped understand delegates and event handlers in .NET. As an ex VB6’er moving to .NET that’s one thing I was having a hard time with.

  • Anonymous

    Good Job
    This article is pretty good to understand event handling but I belive, author should explain little more on delegates.

  • Anonymous

    spot on mate!
    Is there a performance advantage on using delegates rather than raising events?

    Generally should events be used?

    Ta.

    Percival
    pcaburia@cisco.com

  • Anonymous

    great job

    its great post yaa…

    janeshh@gmail.com

  • Anonymous

    Must read!!!
    … for sure

  • Anonymous

    Must read!!!
    … for sure

  • Anonymous

    Nice
    Nice ,
    Any One Must Read This Article

  • Anonymous

    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!

  • Anonymous

    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!
    Must read!!!

  • Anonymous

    Why an ArrayList?
    In your example code, you set up the custom event to maintain an ArrayList of handlers for that event. Why not just use Delegate.Combine and Delegate.Remove to maintain the invocation list of the multicast delegate associated with that event? Wouldn’t the following work?

    Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Private m_MyEvent As MyEventHandler
    Public Custom Event MyEvent As MyEventHandler
    AddHandler(ByVal value As MyEventHandler)
    m_MyEvent = DirectCast( _
    [Delegate].Combine(m_MyEvent, value), _
    MyEventHandler)
    End AddHandler

    RemoveHandler(ByVal value As MyEventHandler)
    m_MyEvent = DirectCast( _
    [Delegate].RemoveAll(m_MyEvent, value), _
    MyEventHandler)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    m_MyEvent.Invoke(sender, e)
    End RaiseEvent
    End Event

  • Anonymous

    Why an ArrayList?
    In your example code, you set up the custom event to maintain an ArrayList of handlers for that event. Why not just use Delegate.Combine and Delegate.Remove to maintain the invocation list of the multicast delegate associated with that event? Wouldn’t the following work?

    Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Private m_MyEvent As MyEventHandler
    Public Custom Event MyEvent As MyEventHandler
    AddHandler(ByVal value As MyEventHandler)
    m_MyEvent = DirectCast( _
    [Delegate].Combine(m_MyEvent, value), _
    MyEventHandler)
    End AddHandler

    RemoveHandler(ByVal value As MyEventHandler)
    m_MyEvent = DirectCast( _
    [Delegate].RemoveAll(m_MyEvent, value), _
    MyEventHandler)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    m_MyEvent.Invoke(sender, e)
    End RaiseEvent
    End Event

  • Gabe

    EventArgs
    good article, you may want to address when creating events to follow the sender/eventarg standard.

  • Anonymous

    Nice
    good article mate i fully understood delegates. Thanks for the help!!

  • Anonymous

    Indeed Great information
    I had tough time understanding this… not its pretty simple for me….

    I respect this great job of making this concept so simple…..

  • Anonymous

    Very good but…
    I am getting blue squigglies under Handlers.Remove(value), Handlers.Add(value), RaiseEvent() method. Thoughts?

  • Ather Ali

    Very good article.
    I found it as a very good and will highly recommend for reading. Concrete and to the point stuff is provided into this article.

  • Anirudh

    Absolutely Good
    Concept is very good and simple…

  • Anonymous

    very good
    can u pls give more details on delegates

  • T.Srinivasa reddy

    pls help me
    i have some doubts.
    1. my task is covert to vb6 to vb.net 2005. how to convert.
    2. for converting any softwares is there?
    3. how to create paint event in vb.net
    pls send me answers to my mail
    pls help me sir………
    Thanks&Regards
    T.Srinivasa Reddy
    09970526736
    srinivas.reddy@ce-n.com

  • Anonymous

    nice artical
    nice artical , on custom event

  • eunice muriithi

    preeety good
    thanks for the easy to follow article

  • Chris O’Brien

    Thanks!
    I hate being a newbie trying to figure out the basics in a new language. This made it so much easier

  • Anonymous

    thanks your article.
    thanks your article..
    very good.

  • Wade Walker

    Some Typos
    Good article but your code had to be adjusted some to work

    Your example code directly above the VisualBasic 8.0 example doesn’t work.

    And instead of >50, in your ‘raise event’ example of the last code example, it should be < 50.

    Thanks!

  • TheLastAnonymous

    thanks
    good paper

  • anil dere

    thanks
    good code

  • Anonymous

    hau
    good