Creating Sub Procedures in

By: Steven Holzner Emailed: 1786 times Printed: 2623 times    

We know all about Sub procedures: They're the handy blocks of code that can organize your code into single-purpose sections to make programming easier. Unlike functions, Sub procedures do not return values, but like functions, you can pass values to Sub procedures in an argument list.

You declare Sub procedures with the Sub statement:

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

Here are the parts of this statement:

  • attrlist-List of attributes for this procedure. You separate multiple attributes with commas.

  • Overloads-Specifies that this Sub procedure overloads one (or more) procedures defined with the same name in a base class. In this case, the argument list must be different from the argument list of every procedure that is to be overloaded (that is, the lists must differ in the number of arguments, their data types, or both). You cannot specify both Overloads and Shadows in the same procedure declaration.

  • Overrides-Specifies that this Sub procedure overrides a procedure with the same name in a base class. Note that the number and data types of the arguments must match those of the procedure in the base class.

  • Overridable-Specifies that this Sub procedure can be overridden by a procedure with the same name in a derived class.

  • NotOverridable-Specifies that this Sub procedure may not be overridden in a derived class.

  • MustOverride-Specifies that this Sub procedure is not implemented. Instead, this procedure must be implemented in a derived class. If it is not, that class will not be creatable.

  • Shadows-Makes this Sub procedure a shadow of an identically named programming element in a base class. A shadowed element is unavailable in the derived class that shadows it. You can use Shadows only at module, namespace, or file level (but not inside a procedure). This means you can declare shadowing variables in a source file or inside a module, class, or structure, but not inside a procedure. Note that you cannot specify both Overloads and Shadows in the same procedure declaration.

  • Shared-Specifies that this Sub procedure is a shared procedure. As a shared procedure, it is not associated with a specific instance of a class or structure, and you can call it 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-Procedures declared Public have public access. There are no restrictions on the accessibility of public procedures.

  • Protected-Procedures declared Protected 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.

  • Friend-Procedures declared Friend have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly.

  • Protected Friend-Procedures declared Protected Friend have both protected and friend accessibility. They can be used by code in the same assembly, as well as by code in derived classes.

  • Private-Procedures declared Private have private access. They are accessible only within their declaration context, including from any nested procedures.

  • name-Name of the Sub procedure.

  • arglist-List of expressions (which can be single variables or simple values) representing arguments that are passed to the Sub procedure when it is called. Multiple arguments are separated by commas. Note that in VB .NET, if you supply an argument list, you must enclose it in parentheses.

  • statements-The block of statements to be executed within the Sub procedure.

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

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

Here are the parts of the arglist:

  • attrlist-List of attributes that apply to this argument. Multiple attributes are separated by commas.

  • Optional-Specifies that this argument is not required when the procedure is called. Note that if you use this keyword, all following arguments in arglist must also be optional and be declared using the Optional keyword. Every optional argument declaration must supply a defaultvalue. Also, Optional cannot be used for any argument if you also use ParamArray.

  • ByVal-Specifies passing by value. In this case, the procedure cannot replace or reassign the underlying variable element in the calling code (unless the argument is a reference type). ByVal is the default in Visual Basic.

  • ByRef-Specifies passing by reference. In this case, the procedure can modify the underlying variable in the calling code the same way the calling code itself can.

  • ParamArray-Used 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-Name of the variable representing the argument.

  • argtype-This part is optional unless Option Strict is set to On, and holds the 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. Note that if the type is Object, or a class, interface, array, or structure, the default value can be only Nothing.

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

<attrname [({ attrargs | attrinit })]>

Here are the parts of attrlist:

  • attrname-Name of the attribute.

  • attrargs-List of positional arguments for this attribute. Multiple arguments are separated by commas.

  • attrinit-List of field or property initializers for this attribute. Multiple initializers are separated by commas.


When you use ByVal (the default in VB .NET), you pass a copy of a variable to a procedure; when you use ByRef, you pass a reference to the variable, and if you make changes to that reference, the original variable is changed.

You call a Sub procedure using the procedure name followed by the argument list. The Exit Sub keywords cause an immediate exit from a Sub procedure. Finally, End Sub ends the procedure definition. Here's an example where I'm passing a text string, "Hello from Visual Basic", to the DisplayMessage Sub procedure, which displays that message in a console application:

Module Module1
    Sub Main()
        DisplayMessage("Hello from Visual Basic")
    End Sub

    Sub DisplayMessage(ByVal strText As String)
    End Sub
End Module

Most Viewed Articles (in )

Latest Articles (in

Comment on this tutorial