Event Statement

   
Event Statement

Syntax

 [   accessmodifier   ] [Shadows] Event   eventName [(arglist)]   [Implements   interfacename   .   interfaceeventname   ] 
accessmodifier (optional; Keyword)

Can be one of Public , Private , Protected , Friend , and Protected Friend

Shadows (optional; Keyword)

Indicates that the event shadows any programming elements of the same name in a base class

eventName (required; String literal)

The name of the event

arglist is optional and has the following syntax:

 [ByVal  ByRef]   varname   [( )] [As   type   ] 
ByVal (optional; Keyword)

The argument is passed by value; that is, a local copy of the variable is assigned the value of the argument.

ByRef (optional; Keyword)

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 .

varname (required; String literal)

The name of the local variable containing either the reference or value of the argument.

type (optional; Keyword)

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.

Implements interfacename . interfaceeventname (optional)

Indicates that the event implements a particular event named interfaceeventname in the interface named interfacename .

Description

Defines a custom event that the object can raise at any time using the RaiseEvent statement.

Rules at a Glance

  • 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.

Example

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 

Programming Tips and Gotchas

  • 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.

See Also

RaiseEvent Statement, Throw Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net