Adding Class Events

When you want to notify a consumer of your class that something has happened inside your class, you can expose the occurrence as an event:

Public Event OnText(ByVal Text As String)

Private Sub DoText(ByVal Text As String)
  RaiseEvent OnText(Text)
End Sub

The LoaderClass (refer to Listing 7.6) raises an OnText event for each line of text read from the input file. The benefit of using events is that the class doesn't need to know which consumers are interested in receiving notification when the event occurs. The LoaderClass code doesn't change regardless of whether any consumer is handling the event.

The statement Public Event OnText(ByVal Text As String) creates a Delegate type implicitly. Any consumer that wants to handle ...

Get Visual Basic® .NET Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.