Event Statement |
[ accessmodifier ] [Shadows] Event eventName [(arglist)] [Implements interfacename . interfaceeventname ]
Can be one of Public , Private , Protected , Friend , and Protected Friend
Indicates that the event shadows any programming elements of the same name in a base class
The name of the event
arglist is optional and has the following syntax:
[ByVal ByRef] varname [( )] [As type ]
The argument is passed by value; that is, a local copy of the variable is assigned the value of the argument.
The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable are reflected in the calling argument. ByRef is the default method of passing variables .
The name of the local variable containing either the reference or value of the argument.
The data type of the argument. It can be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String, Object, or any user - defined type, object type, or data type defined in the BCL.
Indicates that the event implements a particular event named interfaceeventname in the interface named interfacename .
Defines a custom event that the object can raise at any time using the RaiseEvent statement.
The event declaration must be Public so that it is visible outside the object module; it cannot be declared as Friend or Private . However, the Public keyword can be omitted from the declaration, since it is Public by default.
An Event statement can only appear in the Declarations section of an object module, that is, in a form or class module.
The following code snippet demonstrates how you can use an event to communicate a status message back to the client application. To take advantage of this functionality, the client must declare a reference to this class using the WithEvents keyword.
Public Event Status(Message As String) Private Function UpdateRecords( ) as Boolean ... RaiseEvent Status("Opening the database...") ... RaiseEvent Status("Executing the query...") ... RaiseEvent Status("Records were updated...") ... End Function
To allow the client application to handle the event being fired , the object variable must be declared using the WithEvents keyword.
VB custom events do not return a value; however, you can use a ByRef argument in arglist to simulate a return value. For more details, see the RaiseEvent statement.
Unlike parameter lists used with other procedures, Event parameters lists cannot include Optional or ParamArray arguments or default values.
If you use the Event statement in a standard interface class (i.e., a class in which only properties and methods are defined, but no code is included in the procedures) for use with the Implements statement, the Implements statement does not recognize the "outgoing interfaces" used by events, and therefore the event will be ignored.
For more information about implementing your own custom events, see Section 7.2 in Chapter 7.
RaiseEvent Statement, Throw Statement