Sub Statement


Sub Statement

Syntax

     [accessModifier] [procModifier] [Shared] [Shadows] _           Sub name [(Of typeParamName)] ([arglist]) _           [Implements implementsList | Handles eventList]        [statements]        [Exit Sub   | Return]        [statements]     End Sub 


accessModifier (optional)

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

Access level

Description

Public

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

Private

The procedure is accessible only within the defining type.

Protected

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

Friend

The procedure is accessible only within the project that contains the procedure definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Public access level is used.


procModifier (optional)

One of the keywords shown in the following table:

Keyword

Description

Overloads

Indicates that more than one declaration of this subroutine exists, each with a different argument signature

Overrides

For derived classes, indicates that the subroutine overrides a subroutine with the same name and argument signature in the base class

Overridable

Indicates that the subroutine can be overridden in a derived class

NotOverridable

Indicates that the subroutine cannot be overridden in a derived class

MustOverride

Indicates that the subroutine must be overridden in a derived class



Shared (optional)

Indicates that the subroutine is shared and not an instance subroutine. The shared subroutine may be called without a particular instance of the type in which it appears. Shared subroutines are also known as static subroutines.


Shadows (optional)

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


name (required)

The name of the subroutine. If you use the name "New," the procedure will be a constructor. If you use the name "Finalize" and include the Overrides keyword, the procedure will be a destructor.


typeParamName (optional; any)

Adds type parameter placeholders that will later enforce strong typing when the procedure is used. The Of clause implements generics, which are fully described in Chapter 10. If generics will not be used, this clause can be excluded.


arglist (optional; any)

A comma-delimited list of parameters to be supplied to the procedure as arguments from the calling routine.

arglist uses the following syntax and parts:

     [Optional] [ByVal | ByRef] [ParamArray] varname[(  )] _         [As argtype] [= defaultValue] 


Optional (optional)

Flags an argument as optional; optional arguments need not be supplied by the calling routine. All arguments following an optional argument must also be optional. A ParamArray argument cannot be optional.


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.


ParamArray (optional)

The argument is an optional array containing an arbitrary number of elements. It can only be used as the last element of the argument list and cannot be modified by either the ByRef or Optional keywords. If Option Strict is on, the array type must also be specified.


varname (required)

The name of the argument as used in the local procedure.


argtype (optional; Type)

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


defaultValue (optional; any)

For optional arguments, indicates the default value to be supplied when the calling routine does not supply the value. When the Optional keyword is used, this default value is required.


implementsList (optional)

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


eventList (optional)

Comma-separated list of the events handled by this procedure. Each event is in the form eventVariable.eventMember, where eventVariable is a variable declared with the WithEvents keyword, and eventMember is an event member of that variable.


statements (optional)

Program code to be executed within the procedure.

Description

The Sub statement defines a subroutine, including all arguments. Subroutines can appear within classes, structures, or modules. To call a subroutine, specify its name, followed by any arguments in parentheses.

     SomeSubroutine(12, "second argument") 

Usage at a Glance

  • Subroutines cannot be nested; you cannot define one subroutine inside another subroutine.

  • Overloads and Shadows cannot be used in the same declaration.

  • Any number of ExitSub or Return statements can be placed within the subroutine. When these statements are encountered, execution continues with the line of code immediately following the call to the subroutine.

  • The names of a procedure's parameters become the procedure's named arguments.

Version Differences

  • There are several syntax and functionality differences in the declaration of a procedure between VB 6 and the .NET version of VB.

  • In VB 6, arguments to procedures are passed by reference if no passing method is specified. In .NET, the default is to pass by value.

  • If a parameter array is used in VB 6, it is an array of variants. In .NET, all parameter arrays are either of type Object or of some other strong type.

  • In VB 6, parentheses only surrounded the arguments of a procedure call when the Call keyword was used. In .NET, parentheses always surround the arguments.

  • In VB 6, optional arguments do not require that you specify a default value. Instead, the IsMissing function is used to determine whether the optional argument is supplied. In .NET, you must assign a default value to an optional argument.

  • Visual Basic 2005 adds support for generics to subroutines, as discussed in Chapter 10.

See Also

Function 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