Class Statement - Visual Basic (original) (raw)

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.

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

Behavior

Classes and Modules

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

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

See also