O'Reilly logo

Visual Basic 2012 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

SUBROUTINES

A Sub statement defines the subroutine’s name. It declares the parameters that the subroutine takes as arguments and defines the parameters’ data types. Code between the Sub statement and an End Sub statement determines what the subroutine does when it runs.

The syntax for defining a subroutine is as follows:

[attribute_list] [inheritance_mode] [accessibility]
Sub subroutine_name([parameters]) [ Implements interface.subroutine ]
    [ statements ]
End Sub

The following sections describe the pieces of this declaration.

Attribute_List

The optional attribute list is a comma-separated list of attributes that apply to the subroutine. An attribute further refines the definition of a class, method, variable, or other item to give more information to the compiler and the runtime system.

DELIGHTFUL DECORATIONS
Applying an attribute to a class, variable, method, or other code entity is sometimes called decorating the entity.

Attributes are specialized and address issues that arise when you perform very specific programming tasks. For example, the Conditional attribute means the subroutine is conditional upon the definition of some compiler constant. Example program AttributeConditional uses the following code to demonstrate the Conditional attribute:

#Const DEBUG_LIST_CUSTOMERS = True ' #Const DEBUG_LIST_EMPLOYEES = True Private Sub Form1_Load() Handles MyBase.Load ListCustomers() ListEmployees() txtResults.Select(0, 0) End Sub <Conditional("DEBUG_LIST_CUSTOMERS")> Private ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required