NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Sub Statement

Declares the name, arguments, and code that form the body of a Sub procedure.

[Private|Public|Protected|Friend|Protected Friend] Sub name [(arglist)] 
   [statements]
   [Exit Sub]
   [statements]
End Sub

The Sub statement syntax has these parts:

Part Description
Overloads Optional. Indicates that the Sub procedure is overloaded. That is, more than one declaration exists, each with a distinct argument declaration that allows the compiler to distinguish which version to use.
Overrides Optional. Indicates that the Sub procedure overrides an identically named function found in a base class. The data type of the arguments and the return value must exactly match those in the base class procedure.
CantOverride Optional. Indicates that the Sub procedure cannot be overridden in a derived class.
MustOverride Optional. Indicates that a derived class must override the Sub procedure in order to be creatable.
Public Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities.
Private Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.
Protected Optional. Entities declared with the Protected modifier have protected access. Protected access can only be specified on members of types (both regular type members and nested types), although there are different access rules for the two. A protected type member contained in a type is accessible to entities contained in a derived type, provided the access takes place through the derived type. A protected type nested in a type is accessible to entities contained in a derived type, provided the access takes place through the base type (since nested types are not inherited). Protected access is not a superset of friend access.
Friend Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration.
Protected Friend Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.
name Required. Name of the Sub; follows standard variable naming conventions.
arglist Optional. List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas.
statements Optional. Any group of statements to be executed within the Sub procedure.

The arglist argument has the following syntax and parts:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]

Part Description
Optional Optional. Keyword indicating that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
ByVal Optional. Indicates that the argument is passed by value. ByVal is the default in Visual Basic.
ByRef Optional. Indicates that the argument is passed by reference.
ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Object elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. ParamArray can't be used with ByVal, ByRef, or Optional.
varname Required. Name of the variable representing the argument; follows standard variable naming conventions.
type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, , Short, Integer, Long, Single, Double, Decimal, Date, String (variable-length only), Object, or a specific object type. A user-defined type can also be specified for arguments in procedures declared a Private or Friend.
defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.

Remarks

Sub procedures are public by default

All executable code must be in procedures. You can't define a Sub procedure inside another Sub, Function, or Property procedure.

The Exit Sub keywords cause an immediate exit from a Sub procedure. Program execution continues with the statement following the statement that called the Sub procedure. Any number of Exit Sub statements can appear anywhere in a Sub procedure.

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments and perform a series of statements. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.

You call a Sub procedure using the procedure name followed by the argument list. See the Call statement for specific information on how to call Sub procedures.

Variables used in Sub procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.

Caution   A procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you defined at the module level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant or variable, it is assumed that your procedure is referring to that module-level name. To avoid this kind of conflict, explicitly declare variables. You can use an Option Strict statement to force explicit declaration of variables.
Note   You can't use GoTo, or Return to enter or exit a Sub procedure.

See Also

Example

AddressOf Operator | Call Statement | Dim Statement | Friend Keyword | Function Statement | Option Strict