Constructors and Destructors

Constructors and Destructors are specialized event methods that control construction and destruction of objects. Each is specified as a method of the class just like any other method except that the method names Construct_Object and Destroy_Object are used.

Construct_Object

Construct_Object is the name used to declare a constructor method for your class. Constructors are used to create new properties, create nested objects and initialize the object. The syntax for declaring a constructor method is:

Procedure Construct_Object

    {variable declarations}

    {statements}

    Forward Send Construct_Object

    {more statements}

End_Procedure

The first action of a constructor is almost always to call the inherited constructor to create and initialize all the inherited attributes of the object. This is performed by using the Forward Send Construct_Object statement. Refer to Augmenting inherited Methods.

An example of a class declaration with a constructor method is:

Class  cMyEdit  is a Edit

    Procedure Construct_Object

        Forward Send Construct_Object

        Property String psName  "John"

        On_Key  Key_Alt+Key_X  Send Delete_Data

    End_Procedure

End_Class

In this example, the constructor firsts initializes the inherited attributes, then creates a new property, then executes an On_Key statement to declare an accelerator key setting.

Refer to Declaring Properties and Private Objects for more information.

Destroy_Object

Destroy_Object is the name used to declare a destructor method for your class. Destructors are invoked whenever an object is being destroyed. The syntax for declaring a destructor method is:

Procedure Destroy_Object

    {variable declarations}

    {statements}

    Forward Send Destroy_Object

    {more statements}

End_Procedure

The first action of a destructor is almost always to call the inherited destructor. This is performed by using the Forward Send Destroy_Object statement. Refer to Augmenting Inherited Methods.

An example of a class declaration with a destructor method is:

Class  cMyEdit  is a Edit

    Procedure Destroy_Object

        Send Info_Box "We are destroying this object"

        Forward Send Destroy_Object

    End_Procedure

End_Class

Notes

You should never send the messages Construct_Object or Destroy_Object. These are event methods that are automatically executed whenever an object is being created or destroyed. Objects are constructed when an object declaration statement is executed. If you wish to manually destroy an object you should execute the Destroy message.

For more information refer to: the Destroy method in the class reference and Dynamic Objects.