VB.NET Language in a Nutshell

   
Sub Statement

Syntax

[ ClassBehavior ] [ AccessModifier ] Sub name [( arglist )] [ statements ] [Exit Sub] [ statements ] End Sub

ClassBehavior (optional; Keyword)

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 .

AccessModifier (optional)

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

name (required; String literal)

The name of the Sub procedure.

arglist (optional; any)

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 ]

Optional (optional; Keyword)

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.

ByVal (optional; Keyword)

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.

ByRef (optional; Keyword)

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.

ParamArray (optional; Keyword)

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.

varname (required; String literal)

The name of the local variable containing either the reference or value of the argument.

type (optional; Keyword)

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.

defaultvalue (optional; any)

For optional arguments, you must specify a default value.

statements (optional)

Program code to be executed within the procedure.

Description

Defines a subroutine

Rules at a Glance

Programming Tips and Gotchas

VB.NET/VB 6 Differences

See Also

Function Statement

   

Категории