Export (0) Print
Expand All
6 out of 7 rated this helpful - Rate this topic

Sub Statement

Declares the name, arguments, and code that define a Sub procedure.

[ <attrlist> ] [{ Overloads | Overrides | Overridable | 
NotOverridable | MustOverride | Shadows | Shared }] 
[{ Public | Protected | Friend | Protected Friend | Private }] 
Sub name [(arglist)] [ Implements interface.definedname ]
   [ statements ]
   [ Exit Sub ]
   [ statements ]
End Sub

Parts

attrlist
Optional. List of attributes that apply to this procedure. Multiple attributes are separated by commas.
Overloads
Optional. Indicates that this Sub procedure overloads one or more procedures defined with the same name in a base class. The argument list in this declaration must be different from the argument list of every overloaded procedure. The lists must differ in the number of arguments, their data types, or both. This allows the compiler to distinguish which version to use.

You do not have to use the Overloads keyword when you are defining multiple overloaded procedures in the same class. However, if you use Overloads in one of the declarations, you must use it in all of them.

You cannot specify both Overloads and Shadows in the same procedure declaration.

Overrides
Optional. Indicates that this Sub procedure overrides an identically named procedure in a base class. The number and data types of the arguments must exactly match those of the base class procedure.
Overridable
Optional. Indicates that this Sub procedure can be overridden by an identically named procedure in a derived class. Overridable is the default setting for a procedure that itself overrides a base class procedure.
NotOverridable
Optional. Indicates that this Sub procedure cannot be overridden in a derived class. NotOverridable is the default setting for a procedure that does not itself override a base class procedure.
MustOverride
Optional. Indicates that this Sub procedure is not implemented in this class and must be implemented in a derived class for that class to be creatable.
Shadows
Optional. Indicates that this Sub procedure shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. If you shadow a procedure with another procedure, the arguments and the return type do not have to match those in the base class procedure. A shadowed element is unavailable from within the derived class that shadows it, unless the shadowing element is inaccessible, for example if it is Private.

You cannot specify both Overloads and Shadows in the same procedure declaration.

Shared
Optional. Indicates that this Sub procedure is a shared procedure. This means it is not associated with a specific instance of a class or structure. You can call a shared procedure by qualifying it either with the class or structure name, or with the variable name of a specific instance of the class or structure.
Public
Optional. Procedures declared with the Public keyword have public access. There are no restrictions on the accessibility of public procedures.
Protected
Optional. Procedures declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Procedures declared with the Friend keyword have friend access. They are accessible from within their declaration context and from anywhere else in the same program.
Protected Friend
Optional. Procedures declared with the Protected Friend keywords have the union of protected and friend access. They can be used by code in the same program, as well as by code in derived classes. Protected friend access can be specified only on members of classes.
Private
Optional. Procedures declared with the Private keyword have private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.
name
Required. Name of the Sub procedure. Must be a valid Visual Basic identifier.
arglist
Optional. List of variables or expressions representing arguments that are passed to the Sub procedure when it is called. Multiple arguments are separated by commas. If you supply an argument list, you must enclose it in parentheses.
Implements
Optional. Indicates that this Sub procedure implements a Sub procedure defined by an interface.
interface
Required if Implements is supplied. The interface implemented by the class or structure containing this Sub procedure. The class or structure must specify interface in an Implements statement immediately following the Class or Structure statement.
definedname
Required if Implements is supplied. The name by which the Sub procedure is defined in interface. The name of this Sub procedure (in name) does not have to be the same as definedname.
statements
Optional. A block of statements to be executed within the Sub procedure.
End Sub
Terminates the definition of this Sub procedure.

Each argument in the arglist part has the following syntax and parts:

[ <attrlist> ] [ Optional ] [{ ByVal | ByRef }] [ ParamArray ] 
argname[( )] [ As argtype ] [ = defaultvalue ]

arglist Parts

attrlist
Optional. List of attributes that apply to this argument. Multiple attributes are separated by commas.
Optional
Optional. Indicates that this argument is not required when the procedure is called. If this keyword is used, all subsequent arguments in arglist must also be optional and be declared using the Optional keyword. Every optional argument declaration must supply the defaultvalue clause. Optional cannot be used for any argument if ParamArray is used.
ByVal
Optional. Indicates that the procedure cannot replace or reassign the underlying variable element in the calling code. However, if the argument is a reference type, the procedure can modify the contents or members of the underlying object. ByVal is the default in Visual Basic.
ByRef
Optional. Indicates that the procedure can modify the underlying variable in the calling code the same way the calling code itself can.
ParamArray
Optional. Used only as the last argument in arglist to indicate that the final argument is an optional array of elements of the specified type. The ParamArray keyword allows you to pass an arbitrary number of arguments to the procedure. A ParamArray argument is always passed ByVal.
argname
Required. Name of the variable representing the argument. Must be a valid Visual Basic identifier.
argtype
Optional unless Option Strict is On. Data type of the argument passed to the procedure. Can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String; or the name of an enumeration, structure, class, or interface.
defaultvalue
Required for Optional arguments. Any constant or constant expression that evaluates to the data type of the argument. If the type is Object, or a class, interface, array, or structure, the default value can only be Nothing.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

All executable code must be in procedures. You can define a Sub procedure inside a module, class, interface, or structure, but not inside another procedure.

Sub procedures are Public by default. To specify a different accessibility, include Protected, Friend, Protected Friend, or Private in the declaration.

When the Sub procedure returns to the calling program, execution continues with the statement following the statement that called it.

The Exit Sub statement causes an immediate exit from a Sub procedure. Any number of Exit Sub statements can appear anywhere in the procedure.

You can also use the Return statement to immediately exit the procedure, as the following example shows:

Sub MySub(ByVal Q As String)
   ' ...
   Return
   ' ...
End Sub

Any number of Return statements can appear anywhere in the procedure. You can also mix Exit Sub and Return statements.

A Sub procedure, like a Function 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 cannot be used in an expression.

You call a Sub procedure by using the procedure name, followed by the argument list in parentheses, in a statement. You can omit the parentheses only if you are not supplying any arguments. You can optionally use the Call statement to call a Sub procedure. This can improve the readability of your program.

Example

This example uses the Sub statement to define the name, arguments, and code that form the body of a Sub procedure.

' Sub procedure definition.
' Sub procedure with two arguments.
Sub SubComputeArea(ByVal Length As Double, ByVal Width As Double)
   Dim Area As Double   ' Declare local variable. 
   If Length = 0 Or Width = 0 Then
      ' If either argument = 0.
      Exit Sub ' Exit Sub immediately.
   End If
   Area = Length * Width   ' Calculate area of rectangle.
   Debug.WriteLine(Area)   ' Print Area to Immediate window.
End Sub

See Also

Call Statement | Function Statement | Dim Statement | Implements Statement

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.