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
Optional. See Attribute List.
Optional. Can be one of the following:
Optional. See Shadows.
Optional. Indicates a partial definition of the class. See.
Required. Name of this class. See.
Optional. Specifies that this is a generic class.
Optional. Indicates that this class inherits the members of another class. See.
Required if you use the Inherits statement. The name of the class from which this class derives.
Optional. Indicates that this class implements the members of one or more interfaces. See.
Required if you use the Implements statement. The names of the interfaces this class implements.
Optional. Statements which define the members of this class.
- End Class
Required. Terminates the Class definition.
A Class statement defines a new data type. A class is a fundamental building block of object-oriented programming (OOP). For more information, seeand .
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.
Each instance of a class has a lifetime independent of all other instances. This lifetime begins when it is created by aclause or by a function such as . It ends when all variables pointing to the instance have been set to or to instances of other classes.
Classes default toaccess. You can adjust their access levels with the access modifiers. For more information, see .
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, 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 interfacenames. An exception to this is reimplementation of a base class member. For more information, see "Reimplementation" in ., you must implement every member defined by every interface you specify in
Default Property. A class can specify at most one property as its default property. For more information, see.
Access Level. Within a class, you can declare each member with its own access level. Class members default toaccess, except variables and constants, which default to 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.
Class members have lifetimes depending on how and where they are declared. For more information, see.
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. For more information, see.
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.
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