Event Statement
Declares a user-defined event.
Syntax
[Public] Event procedurename [(arglist)]
The Event statement has these parts:
| Part | Description |
|---|---|
| Public | Optional. Specifies that the Event visible throughout the project.Events types are Public by default. Note that events can only be raised in the module in which they are declared. |
| procedurename | Required. Name of the event; follows standard variable naming conventions. |
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname[( )] [As type]
| Part | Description |
|---|---|
| ByVal | Optional. Indicates that the argument is passed by value. |
| ByRef | Optional. Indicates that the argument is passed by reference.ByRef is the default in Visual Basic. |
| varname | Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions. |
| type | Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (variable length only), Object, Variant, a user-defined type, or an object type. |
Remarks
Once the event has been declared, use the RaiseEvent statement to fire the event. A syntax error occurs if an Event declaration appears in a standard module. An event can't be declared to return a value. A typical event might be declared and raised as shown in the following fragments:
|
Note
|
|---|
| You can declare event arguments just as you do arguments of procedures, with the following exceptions: events cannot have named arguments, Optional arguments, or ParamArray arguments. Events do not have return values. |
Example
The following example uses events to count off seconds during a demonstration of the fastest 100 meter race. The code illustrates all of the event-related methods, properties, and statements, including the Event statement.
The class that raises an event is the event source, and the classes that implement the event are the sinks. An event source can have multiple sinks for the events it generates. When the class raises the event, that event is fired on every class that has elected to sink events for that instance of the object.
The example also uses a form (
|
|
|
|
|
The code for
|
|
The remaining code is in a class module named TimerState. The Event statements declare the procedures initiated when events are raised.
|
Note