Class Statement

Declares the name of a class and defines its implementation.

Syntax

[ Abstract ] Class name [ < typeList > ] [ Is superclass ] [ Does trait ]...
    [ Where genericConstraints ] ...
    [ statements ]
End Class

Parts

Abstract
Optional — Specifies that this is an abstract class, and is ordinarily used as the parent of a concrete class.
name
Required — An identifier that names the class.
typeList
Optional — One or more identifiers separated by commas and surrounded by angle brackets (< >).
superclass
Optional — The name of another class from which this class inherits methods and properties.
Does
Optional, repeatable — Specifies which traits a class implements.
trait
The name of a trait, a way to define behavior. See Does Clause (Traits) for more information.
Where
Optional, repeatable — Specifies constraints to place on type arguments. Requires a typeList. You can place one at the end of the first line. Or you can place one or more on separate lines before any statements.
genericConstraints
One or more constraints on a generic parameter separated by commas. See Where Clause (Generics) for details.
statements
Optional — The statements that define the members of the class.
End Class
Terminates the Class block.

Remarks

A Class statement defines a data type known as a class. Classes are fundamental building blocks in object-oriented programming (OOP). See Objects and Classes for more information.

You can place a Class only within certain contexts. These contexts include Program and Library modules, other classes, and Object and Trait declarations. You cannot place a class within a procedure. See Declaration Contexts and Default Access Levels for more information.

Examples

Listing 1
Program ClassExample
Require IO #io

Class Person
    Dim myName As String, myAge As UShort
    Constructor name As String, age As UShort
        myName = name
        myAge = age
    End Constructor
    Method GetName As String
        Return myName
    End Method
    Method GetAge As UShort
        Return myAge
    End Method
End Class

Person #a, "Alice", 24
Person #b, "Bob", 42

#io.WriteLine #a.GetName & " is " & #a.GetAge
#io.WriteLine #b.GetName & " is " & #b.GetAge

End

' Output:
' Alice is 24
' Bob is 42

See also