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.
Syntax
[ <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: - Public - Protected - Friend - Private - Protected Friend - Private Protected See Access levels in Visual Basic. |
Shadows |
Optional. See Shadows. |
MustInherit |
Optional. See MustInherit. |
NotInheritable |
Optional. See NotInheritable. |
Partial |
Optional. Indicates a partial definition of the class. See Partial. |
name |
Required. Name of this class. See Declared Element Names. |
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.
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.
Classes default to Friend 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 aFriend
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.Default Property. A class can specify at most one property as its default property. For more information, see Default.
Behavior
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.
Example
The following example uses a Class
statement to define a class and several members.
Class BankAccount
Shared interestRate As Decimal
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