Class Statement

Declares the name of a class and gives its implementation.

Syntax

[ modifier ] _
Class class_name [ type_list ] [ Is base_class ] [ Does trait_list ]
    [ Where generic_constraints ]…
    [ statements ]
End [ Class ]

Parts

modifier
Optional one of these:
  • @Abstract – The class can only be the base class (base_class) of other classes. See @Abstract.
  • @Deprecated – The class can be removed in a subsequent version. See @Deprecated.
  • @Open – The class can be the base class (base_class) of other classes. See @Open.
class_name
Mandatory name for the class.
type_list
Optional one or more type parameters, with a comma between each, all between brackets ([ ]). See Type List for more information.
Is
A keyword before base_class.
You can also use one of the non-reserved keywords Extends or Inherits as an alternative.
base_class
Optional name of a class that supplies its public elements (typically methods and properties) to this class.
trait_list
Mandatory after Does, one or more traits, with a comma between each. See Does Clause (Traits) for more information.
Where
Optional keyword you can use again and again. You can put one at the end of the first line, or one or more on different lines.
generic_constraints
One or more limits (or constraints) on a generic parameter, with a comma between each. See Where Clause (Generics) for more information.
statements
Optional statements that are the members of the class.
End
Completes the statement. You can also use End Class.
You can change the syntax of this part. See @Option Directive for more information.

Instructions

Class is a statement that makes a data type known as a class. Classes are important constructs in object-oriented programming (OOP). See Objects and Classes for more information.

You can put a class only in some contexts. These contexts include modules (Program and Library), other classes, and the constructs Object and Trait. You cannot put a class in a procedure. See Declaration Contexts and Default Access Levels for more information.

Permitted members

Changes in syntax with @Option

There are two areas where you can change the syntax of the statement:

See @Option Directive for more information.

Examples

Example 1

Require ViviFire.IO

Class Person
    // Private members.
    Constructor Var my_name As String
        // Validate here.
    End Constructor

    // Public members.
    Method SayHello
        PrintLine $"Hello, I'm {my_name}!"
    End Method
End Class

New Person a, "Alice"
New Person b, "Bob"

a.SayHello
b.SayHello
Output:
Hello, I'm Alice!
Hello, I'm Bob!

See also