Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises.
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _ Class name [ ( Of typelist ) ] [ Inherits classname ] [ Implements interfacenames ] [ statements ] End Class
||Optional. See Attribute List.|
||Optional. Can be one of the following:
See Access levels in Visual Basic.
||Optional. See Shadows.|
||Optional. See MustInherit.|
||Optional. See NotInheritable.|
||Optional. Indicates a partial definition of the class. See Partial.|
||Required. Name of this class. See Declared Element Names.|
||Optional. Specifies that this is a generic class.|
||Required if you use the Of keyword. List of type parameters for this class. See Type List.|
||Optional. Indicates that this class inherits the members of another class. See Inherits Statement.|
||Required if you use the
||Optional. Indicates that this class implements the members of one or more interfaces. See Implements Statement.|
||Required if you use the
||Optional. Statements which define the members of this class.|
||Required. Terminates the
Class statement defines a new data type. A class is a fundamental building block of object-oriented programming (OOP). For more information, see Objects and Classes.
You can use
Class only at namespace or module level. This means the declaration context for a class must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure or block. For more information, see Declaration Contexts and Default Access Levels.
Each instance of a class has a lifetime independent of all other instances. This lifetime begins when it is created by a New Operator clause or by a function such as CreateObject. It ends when all variables pointing to the instance have been set to Nothing or to instances of other classes.
Nesting. You can define one class within another. The outer class is called the containing class, and the inner class is called a nested class.
Inheritance. If the class uses the Inherits Statement, you can specify only one base class or interface. A class cannot inherit from more than one element.
A class cannot inherit from another class with a more restrictive access level. For example, a
Publicclass cannot inherit from a
A class cannot inherit from a class nested within it.
Implementation. If the class uses the Implements Statement, you must implement every member defined by every interface you specify in
interfacenames. An exception to this is reimplementation of a base class member. For more information, see "Reimplementation" in Implements.
Default Property. A class can specify at most one property as its default property. For more information, see Default.
Access Level. Within a class, you can declare each member with its own access level. Class members default to Public access, except variables and constants, which default to Private access. When a class has more restricted access than one of its members, the class access level takes precedence.
Scope. A class is in scope throughout its containing namespace, class, structure, or module.
The scope of every class member is the entire class.
Lifetime. Visual Basic does not support static classes. The functional equivalent of a static class is provided by a module. For more information, see Module Statement.
Class members have lifetimes depending on how and where they are declared. For more information, see Lifetime in Visual Basic.
Qualification. Code outside a class must qualify a member's name with the name of that class.
If code inside a nested class makes an unqualified reference to a programming element, Visual Basic searches for the element first in the nested class, then in its containing class, and so on out to the outermost containing element.
Classes and Modules
These elements have many similarities, but there are some important differences as well.
Terminology. Previous versions of Visual Basic recognize two types of modules: class modules (.cls files) and standard modules (.bas files). The current version calls these classes and modules, respectively.
Shared Members. You can control whether a member of a class is a shared or instance member.
Object Orientation. Classes are object-oriented, but modules are not. You can create one or more instances of a class. For more information, see Objects and Classes.
The following example uses a
Class statement to define a class and several members.
Class bankAccount Shared interestRate As Decimal Private accountNumber As String Private accountBalance As Decimal Public holdOnAccount As Boolean = False Public ReadOnly Property balance() As Decimal Get Return accountBalance End Get End Property Public Sub postInterest() accountBalance = accountBalance * (1 + interestRate) End Sub Public Sub postDeposit(ByVal amountIn As Decimal) accountBalance = accountBalance + amountIn End Sub Public Sub postWithdrawal(ByVal amountOut As Decimal) accountBalance = accountBalance - amountOut End Sub End Class
Objects and Classes
Structures and Classes
Object Lifetime: How Objects Are Created and Destroyed
Generic Types in Visual Basic
How to: Use a Generic Class