Class Statement (Visual Basic)
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
Parts
Term |
Definition |
attributelist |
Optional. See Attribute List. |
accessmodifier |
Optional. Can be one of the following: |
Shadows |
Optional. See Shadows. |
MustInherit |
Optional. See MustInherit (Visual Basic). |
NotInheritable |
Optional. See NotInheritable (Visual Basic). |
Partial |
Optional. Indicates a partial definition of the class. See Partial (Visual Basic). |
name |
Required. Name of this class. See Declared Element Names (Visual Basic). |
Of |
Optional. Specifies that this is a generic class. |
typelist |
Required if you use the Of keyword. List of type parameters for this class. See Type List. |
Inherits |
Optional. Indicates that this class inherits the members of another class. See Inherits Statement. |
classname |
Required if you use the Inherits statement. The name of the class from which this class derives. |
Implements |
Optional. Indicates that this class implements the members of one or more interfaces. See Implements Statement. |
interfacenames |
Required if you use the Implements statement. The names of the interfaces this class implements. |
statements |
Optional. Statements which define the members of this class. |
End Class |
Required. Terminates the Class definition. |
Remarks
A 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 in Visual Basic.
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 (Visual Basic).
Each instance of a class has a lifetime independent of all other instances. This lifetime begins when it is created by a New Operator (Visual Basic) clause or by a function such as CreateObject. It ends when all variables pointing to the instance have been set to Nothing (Visual Basic) or to instances of other classes.
Classes default to Friend (Visual Basic) access. You can adjust their access levels with the access modifiers. For more information, see Access Levels in Visual Basic.
Rules
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 Public class cannot inherit from a Friend class.
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 Clause (Visual Basic).
Default Property. A class can specify at most one property as its default property. For more information, see Default (Visual Basic).
Behavior
Access Level. Within a class, you can declare each member with its own access level. Class members default to Public (Visual Basic) access, except variables and constants, which default to Private (Visual Basic) 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 in Visual Basic.
Example
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
See Also
Tasks
How to: Use a Generic Class (Visual Basic)
Reference
Interface Statement (Visual Basic)
Concepts
Structures and Classes (Visual Basic)
Object Lifetime: How Objects Are Created and Destroyed (Visual Basic)
Generic Types in Visual Basic (Visual Basic)