Last Updated:

TObject Class | Delphi | Tutorial

The TObject class is the ancestor of the entire hierarchy of VCL classes used in Delphi. It implements functions that any object that can be created in the development environment will necessarily perform. Given the huge variety of possible applications of objects in the process of creating applications, we can say that the range of operations common to all classes of operations is very small.

First of all, it is the creation of an instance of the object and its destruction. Any object performs these two operations without fail. The process of creating an object includes selecting an address space area, setting a pointer to an instance of an object, setting initial property values, and performing installation actions related to the object's purpose. In general, the last two operations may not be performed.

A pointer to an instance of an object is passed to a variable of object type, which will later identify the object in the application's code. In the code snippet above, a variable of object type someList is declared as an instance of type TStrings. When you create an instance of this type, the Create constructor returns a pointer to the memory allocated to the new object variable. To do this, use the Newinstance method, which is called automatically in the constructor:

class function Newinstance: TObject; virtual;

An object of the TObject class ensures that any object derived from it executes this process. And already inside the constructor, which is inherited from the TObject class, you can provide for the initialization of variables and the execution of additional operations. The designer declaration is as follows:

constructor Create;

In descendant constructors, this declaration can overlap, but if necessary, call the ancestor constructor, use the inherited operator:

constructor TSomeObject.Create;
inherited Create;

To destroy an instance of an object in the TObject class, use the Destroy and Free methods:

destructor Destroy; virtual;
procedure Free;

As you can see from the announcement, the real destructor is the Destroy method. It releases all resources occupied by the object instance. Typically, when creating new classes, the destructor always overlaps in order to correctly complete work with the data.

Note that you typically call the inherited constructor first. This is necessary to perform all the necessary operations when you create an object before initializing its own properties. When an object is destroyed, the final operations are usually performed first, and only at the very end is the inherited destructor called to actually destroy the object

When destroying objects, it is recommended that you use the Free method instead of the destructor, which simply calls the destructor, but first verifies that the pointer to the object instance is not empty (not equal to Nil). This avoids serious mistakes.

If an object owns other objects (for example, a form owns all the components hosted on it), its Free method will automatically call the same methods for all objects. Therefore, when closing the form, the developer is relieved of the need to worry about the destruction of all components.

To free the memory occupied by the object, the destructor automatically calls the Freelnstance method:

procedure Freelnstance; virtual;

Each object should contain some information about itself that is used by the application and the development environment. Therefore, the TObject class contains a number of methods that enable descendants to represent this information.

Class function Classlnfo: Pointer method; returns a pointer to the Runtime Information (RTTI) table. This information is used in the development environment and in the application.

Class function ClassName: ShortString; returns the type name of an object that can be used for identification. For example, a single button click handler method can work with several types of button components:

procedure TForml.BitBtnlClick(Sender: TObject);
if Sender is TBitBtn
then TBitBtn(Sender).Enabled: = False;
if Sender is TSpeedButton
then TSpeedButton(Sender).Down: = True;

Class function ClassNamels(const Name: string): Boolean; allows you to determine whether this object is of the type whose name is passed in the Name parameter. If the answer is yes, the function returns True.

As you know, programming for Windows is based on events. Every application and every software object must be able to respond to an event message and, in turn, send out messages. The third function common to all objects is to perform these operations.

Procedure Dispatch(var Message): virtual; handles messages received by the object. It determines whether the object can process the message using its own event handlers. In the absence of such methods, the message is passed to the dispatch method of the ancestor class (if any).

The TObject class has a predefined event handler:

procedure DefaultHandler(var Message); virtual;

In addition to the methods discussed here, the TObject class has several other methods that are primarily used to interact with the development environment.

In general, the TObject class can be used to create some simple classes based on it for use in applications.