Sub Statement |
[ ClassBehavior ] [ AccessModifier ] Sub name [( arglist )] [ statements ] [Exit Sub] [ statements ] End Sub
One of the keywords shown in the following table:
Keyword | Description |
---|---|
Overloads | Indicates that more than one declaration of this subroutine exists (with different argument signatures). |
Overrides | For derived classes, indicates that the subroutine overrides the subroutine by 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. |
Shadows | In a derived class definition, indicates that calls to derived class members that are made through a base class ignore the shadowed implementation. |
Shared | Callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static subroutines . |
The possible values of AccessModifier are Public , Private , Friend , Protected , or Protected Friend . The following table describes the effects of the various access modifiers. Note that "direct access" refers to accessing the member without any qualification, as in:
classvariable = 100
and "class/object access" refers to accessing the member through qualification, either with the class name or the name of an object of that class. For more information, see Section 4.7 in Chapter 4.
Direct access scope | Class/object access scope | |
---|---|---|
Private | Declaring class | Declaring class |
Protected | All derived classes | Declaring class |
Friend | Derived in-project classes | Declaring project |
Protected Friend | All derived classes | Declaring project |
Public | All derived classes | All projects |
The name of the Sub procedure.
A comma-delimited list of variables to be passed to the sub procedure as arguments from the calling procedure.
arglist uses the following syntax and parts :
[Optional] [ByVal ByRef] [ParamArray] varname [( )] _ [As type ] [= defaultvalue ]
An optional argument is one that need not be supplied when calling the function. However, all arguments following an optional one must also be optional. A ParamArray argument cannot be optional.
The argument is passed by value; that is, the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables.
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 will be also reflected in the calling argument.
Indicates that 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.
The name of the local variable containing either the reference or value of the argument.
The data type of the argument. It can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, a user - defined type, or an object type.
For optional arguments, you must specify a default value.
Program code to be executed within the procedure.
Defines a subroutine
Subroutines cannot be nested; that is, you cannot define one subroutine inside another subroutine. (This applies to all procedures.)
If you do not include one of the accessmodifier keywords, a subroutine will be Public by default.
Any number of Exit Sub statements can be placed within the subroutine. Execution will continue with the line of code immediately following the call to the subroutine.
If you specify an optional parameter in your subroutine declaration, you must also provide a default value for that parameter. For example:
Private Sub ShowMessage(Optional sMsg _ As String = "Not given")
A subroutine is called by using its name and enclosing any arguments in parentheses. For example, a routine named SomeRoutine might be called as follows :
x = 12 y = 12 SomeRoutine(x, y)
Note that because it does not return a value, a subroutine cannot be assigned to a variable. For example, the following is illegal:
z = SomeRoutine(x, y)
There is often confusion between using the ByRef and ByVal methods of assigning arguments to the Sub procedure. ByRef assigns the reference of the variable in the calling procedure to the variable in the Sub procedure; that is, it passes a pointer containing the address in memory of the variable in the calling procedure. As a result, any changes made to the variable from within the Sub procedure are in reality made to the variable in the calling procedure. On the other hand, ByVal assigns the value of the variable in the calling procedure to the variable in the Sub procedure; that is, it makes a separate copy of the variable in a separate memory location. Changes made to the variable in the Sub procedure have no effect on the variable in the calling procedure. In general, ByRef arguments within class modules take longer to handle, since marshaling back and forth between Sub procedure and calling module must take place. So unless you explicitly need to modify a variable's value within a Sub procedure, it's best to pass parameters by value.
The names of procedure parameters become the procedure's named arguments. Because of this, it is best to use meaningful names for parameters, and to avoid the use of Hungarian notation.
If you do not specify whether an individual element in arglist is passed ByVal or ByRef , it is passed by reference in VB 6. In VB.NET, it is passed by value.
If a parameter array is used in VB 6, it is an array of variants. In VB.NET, since the Variant is no longer supported, it must be an array of objects or a strongly typed array.
In VB 6, a Sub procedure was called either by using the Call statement and including procedure arguments in parentheses or by using the name of the procedure and including arguments without parentheses. VB.NET features a standard calling syntax in which arguments are always enclosed in parentheses.
Function Statement