Event Statement


Event Statement

Syntax

     [accessModifier] [Shadows] Event name ([arglist]) _        [Implements implementsList] 

or:

     [accessModifier] [Shadows] Event name As delegateName _        [Implements implementsList] 


accessModifier (optional)

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

Access level

Description

Public

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

Private

The event is accessible only within the defining type.

Protected

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

Friend

The event is accessible only within the project that contains the 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 event shadows an identically named element in a base class.


name (required)

The name of the event.


arglist (optional; any)

A comma-delimited list of parameters to be supplied to the event as arguments when the event is fired.

arglist 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, although event handlers need not retain this name.


argtype (optional; Type)

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


implementsList (optional)

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


delegateName (optional)

New in 2005. A delegate with an argument signature that is used as the argument signature of this event.

Description

The Event statement defines an event that the containing type can raise at any time using the RaiseEvent statement. Events can appear within classes, structures, and modules.

Usage at a Glance

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

  • All events for forms and controls in the .NET Framework's Windows Forms package share a common argument signature:

         Public Event name (ByVal sender As Object, _        ByVal e As System.EventArgs) 

  • VB events do not return a value; however, you can use the ByRef keyword in arglist to return data from the event through a parameter.

Example

The following example implements a simple class with one event, which is triggered through the UpdateRecords procedure.

     Friend Class EventLadenClass        Public Event StatusChanged(ByVal message As String)        Public Sub UpdateRecords(  )           RaiseEvent StatusChanged("Records are being updated.")        End Sub     End Class 

Version Differences

  • Visual Basic 2005 adds the As delegateName clause to the Event statement, a new way of indicating the argument signature for an event.

  • Visual Basic 2005 adds a Custom Event statement that includes greater control over the lifetime of an event. See the "Custom Event Statement" entry in this chapter for additional information.

See Also

Custom Event Statement, RaiseEvent 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