O'Reilly logo

Programming Visual Basic .NET by Dave Grundgeiger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Events

An event is a callback mechanism. With it, objects can notify users that something interesting has happened. If desired, data can be passed from the object to the client as part of the notification. Throughout this section, I use the terms event producer , producer class , and producer object to talk about a class (and its instances) capable of raising events. I use the terms event consumer , consumer class , and consumer object to talk about a class (and its instances) capable of receiving and acting on events raised by an event producer.

Here is a class that exposes an event:

Public Class EventProducer

   Public Event SomeEvent(  )

   Public Sub DoSomething(  )
      ' ...
      RaiseEvent SomeEvent(  )
      ' ...
   End Sub

End Class

The Event statement in this code fragment declares that this class is capable of raising an event called SomeEvent. The empty parentheses in the declaration indicate that the event will not pass any data. An example later in this section will show how to define events that pass data.

The RaiseEvent statement in the DoSomething method raises the event. Any clients of the object that have registered their desire to receive this event will receive it at this time. Receiving an event means that a method will be called on the client to handle the event. Here is the definition of a client class that receives and handles events from the EventProducer class:

Public Class EventConsumer Private WithEvents producer As EventProducer Public Sub producer_SomeEvent( ) Handles producer.SomeEvent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required