Custom Event Statement


Custom Event Statement

Syntax

     [accessModifier] [Shadows] Custom Event name As delegateName _           [Implements implementsList]        AddHandler(ByVal value As delegateName)           [statements]        End AddHandler        RemoveHandler(ByVal value As delegateName)           [statements]        End RemoveHandler        RaiseEvent(delegateSignature)           [statements]        End RaiseEvent     End Event 


accessModifier (optional)

Specifies the scope and accessibility of the custom event. One of the following access levels:

Access level

Description

Public

The custom event is publicly accessible anywhere, both inside and outside of the project.

Private

The custom event is accessible only within the defining type.

Protected

The custom event is accessible only to the code in the defining type or to one of its derived types.

Friend

The custom event is accessible only within the project that contains the custom event definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Public access level is used.


Shadows (optional)

Indicates that the custom event shadows an identically named element in a base class.


name (required)

The name of the custom event.


implementsList (optional)

Comma-separated list of the interface members implemented by this custom event.


delegateName (required)

A delegate with an argument signature that is used as the argument signature of this custom event.


value (required)

The name of the accessor argument. By convention, this argument is named "value." The argument is always of type delegateName.


statements (optional)

Program code to be executed within the AddHandler, RemoveHandler, and RaiseEvent accessors.


delegateSignature (optional; any)

A comma-delimited list of parameters to be supplied to the custom event as arguments when the event is raised. This argument list must match the one defined by delegateName.

delegateSignature uses the following syntax and parts:

     [ByVal | ByRef] varname[(  )] [As argtype] 


ByVal (optional)

The argument is passed by value; the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables.


ByRef (optional)

The argument is passed by reference; the local variable is a reference to the argument being passed. All changes made to the local variable will also be reflected in the calling argument.


varname (required)

The name of the argument.


argtype (optional; Type)

The data type of the argument. Any valid .NET data type can be used.

Description

New in 2005. The Custom Event statement defines a custom event that the containing type can raise at any time using the RaiseEvent statement. As event handlers for this event are added, removed, or called (raised), one the three accessors (AddHandler, RemoveHandler, and RaiseEvent) is called respectively, immediately before the actual related action occurs. This allows the designer of the event to exert more control over the lifetime of the event.

Usage at a Glance

  • To handle events, an object must be declared with the WithEvents keyword.

  • To declare an event without the additional AddHandler, RemoveHandler, and RaiseEvent accessors, use the standard Event statement. See the Event Statement entry in this chapter for more information.

Version Differences

The Custom Event statement is new with Visual Basic 2005.

See Also

Event Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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