O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

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

Name

RaiseEvent Statement

Syntax

RaiseEvent eventName([arglist])
eventName (required; String literal)

The name of the event

arglist (optional; any (defined by the Event statement)

A comma-delimited list of arguments

Description

Generates a predefined, custom event within any procedure of an object module

Rules at a Glance

  • eventName must already be defined in the Declarations section of the module using the Event statement.

  • arglist must match the number and data type of parameters defined in the Event statement and must be surrounded by parentheses.

  • The RaiseEvent and Event statements can only be used in class modules and not in standard modules.

Example

The following code snippet demonstrates how you can use an event to communicate a status message back to the client application and, at the same time, use a ByRef argument to trap a user response in the client application. This gets around the fact that events can’t return values. To take advantage of this functionality, the client must declare a reference to this class using the WithEvents keyword:

Public Class CTransact Public Event Status(Message As String, _ ByRef Cancel As Boolean) Public Function UpdateRecords(iVal As Integer) as Boolean Dim blnCancel As Boolean = False If iVal > 1000 Then RaiseEvent Status("Is value too high?", blnCancel) If blnCancel Then Console.WriteLine("Abandoning operation...") Exit Function Else iVal = 1000 End If End If console.writeline(iVal) End Function End Class Module modMain Public WithEvents oTran ...

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