Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata