Module Statement - Visual Basic (original) (raw)

Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises.

Syntax

[ <attributelist> ] [ accessmodifier ]  Module name
    [ statements ]
End Module

Parts

attributelist
Optional. See Attribute List.

accessmodifier
Optional. Can be one of the following:

See Access levels in Visual Basic.

name
Required. Name of this module. See Declared Element Names.

statements
Optional. Statements which define the variables, properties, events, procedures, and nested types of this module.

End Module
Terminates the Module definition.

A Module statement defines a reference type available throughout its namespace. A module (sometimes called a standard module) is similar to a class but with some important distinctions. Every module has exactly one instance and does not need to be created or assigned to a variable. Modules do not support inheritance or implement interfaces. Notice that a module is not a type in the sense that a class or structure is — you cannot declare a programming element to have the data type of a module.

You can use Module only at namespace level. This means the declaration context for a module must be a source file or namespace, and cannot be a class, structure, module, interface, procedure, or block. You cannot nest a module within another module, or within any type. For more information, see Declaration Contexts and Default Access Levels.

A module has the same lifetime as your program. Because its members are all Shared, they also have lifetimes equal to that of the program.

Modules default to Friend access. You can adjust their access levels with the access modifiers. For more information, see Access levels in Visual Basic.

All members of a module are implicitly Shared.

Classes and Modules

These elements have many similarities, but there are some important differences as well.

Rules

Behavior

Example

Public Module thisModule
    Sub Main()
        Dim userName As String = InputBox("What is your name?")
        MsgBox("User name is " & userName)
    End Sub
    ' Insert variable, property, procedure, and event declarations.
End Module

See also