Using Constructors and Destructors

A constructor is a method that is executed every time a new instance of a class is created.

To identify a method as a constructor, name the method create. Use a constructor method to set defaults when an object is created, as shown in the following example.

create() updating;
begin
    self.bevelInner    := 0;             // none
    self.bevelOuter    := 0;             // none
    self.boundaryWidth := 0;
    self.boundaryBrush := 0;             // black
    self.caption       := null;
    self.borderStyle   := 0;
    self.backColor     := 15269887;      // buff
end;

A destructor is a method that is executed every time an instance of a specific class is deleted.

To identify a method as a destructor, name the method delete. Use a destructor method for cleanup tasks when an object is deleted, as shown in the following example.

delete() updating;
vars
    tran : Transaction;
begin
    foreach tran in self.myTransactions do
        sharedLock(tran);
        delete tran;
    endforeach;
end;

Constructors and destructors for all superclasses are automatically invoked, to ensure that you do not compromise the behavior of superclasses.

Destructor methods must not have parameters. For details about using a constructor with parameters, see "create Method Constructors", in the following subsection.

You cannot specify constructors and destructors for an application class or a global class (that is, a subclass of the Application class or Global class, respectively).

Constructors are called starting with the highest superclass implementation and continuing down the hierarchy to the current class. Destructors are executed in the reverse order, starting with the current class and continuing up through the hierarchy to the highest superclass. To ensure that a form has no orphaned children when deleting a window, the lowest child in the hierarchy is deleted first, followed by the next lowest child, and so forth up the hierarchy until the parent is deleted. You therefore do not need to handle the deletion of a form's children in your code, as the child or children no longer exist when the parent is deleted.