A class declaration including constructor code, which typically creates member properties, and class methods.

Syntax

CLASS <class name>[(<parameters>)]
 [OF <superclass name>[(<parameters>)]
  [CUSTOM]
  [FROM <filename expC>] ]


 [PROTECT <propertyList>]
 [<constructor code>]
 [<methods>]
ENDCLASS

<class name>

The name of the class. Although dBASE Plus imposes no limit to the length of class names, it recognizes only the first 32 characters.

OF <superclass name>

Indicates that the class is a derived class that inherits the properties defined in the superclass. The superclass constructor is called before the <constructor code> in the current CLASS is called, which means that any properties created in the superclass are inherited by the class.

<parameters>

Optional parameters to pass to the class, and through to the superclass.

CUSTOM

Identifies the class as a custom component class, so that its predefined properties are not streamed out by the visual design tools.

FROM <filename>

<filename> specifies the file containing the definition code for the <superclass>, if the <superclass> is not defined in the same file as the class.

PROTECT <propertyList>

<propertyList> is a list of properties and/or methods of the class which are to be accessible only by other members of the class, and by classes derived from the class.

<constructor code>

The code that is called when a new instance of the class is created with the NEW operator or a DEFINE statement. The constructor consists of all the code at the top of the class declaration up to the first method.

<methods>

Any number of functions designed for the class.

ENDCLASS

A required keyword that marks the end of the CLASS structure.

Description

Use CLASS to create a new class.

A class is a specification, or template, for a type of object. dBASE Plus provides many stock classes, such as Form and Query; for example, when you create a form, you are creating a new Form object that has the standard properties and methods from the Form class. However, when you declare a class with CLASS, you specify the properties and methods of objects derived from the new class.

A CLASS declaration formalizes the creation of an object and its methods. Although you can always add properties to an object and assign methods dynamically, a CLASS simplifies the task and allows you to build a clear class hierarchy.

Another benefit is polymorphism. Every FUNCTION (or PROCEDURE) defined in the CLASS becomes a method of the class. An object of that class automatically has a property with the same name as each FUNCTION that contains a reference to that FUNCTION. Because a method is part of the CLASS, different functions may use the same name as long as they are methods of different classes. For example, you can have multiple copy( ) functions in different classes, with each one applying to objects of that class. Without classes, you would have to name the functions differently even if they performed the same task conceptually.

Before the first statement in the constructor is executed, if the CLASS extends another class, the constructor for that superclass has already been executed, so the object contains all the superclass properties. Any properties that refer to methods, as described in the previous paragraph, are assigned. This means that if the CLASS contains a method with the same name as a method in a superclass, the method in the CLASS overrides the method in the superclass. The CLASS constructor, if any, then executes.

In the constructor, the variable this refers to the object being created. Typically, the constructor creates properties by assigning them to this with dot notation. However, the constructor may contain any code at all, except another CLASS—you can’t nest classes—or a FUNCTION, since that FUNCTION would become a method of the class and indicate the end of the constructor.

Properties and methods can be protected to prevent the user of the class from reading or changing the protected property values, or calling the protected methods from outside of the class.