Programming Delphi TObject, TPersistent and TComponent Branch of VCL/CLX Descriptions
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/ on line 149

The TObject Branch

The TObject branch includes all VCL and CLX objects that descend from TObject but not from TPersistent. Much of the powerful capability of VCL and CLX objects are established by the methods that TObject introduces. TObject encapsulates the fundamental behavior common to all objects in the VCL and CLX by introducing methods that provide:

  • The ability to respond when objects are created or destroyed.
  • Class type and instance information on an object, and runtime type information (RTTI) about its published properties.
  • Support for message-handling (VCL) or system events (CLX).

TObject is the immediate ancestor of many simple classes. Classes that are contained within the TObject branch have one common, important characteristic: they are transitory. What this means is that these classes do not have a method to save the state that they are in prior to destruction; they are not persistent.
One of the main groups of classes in this branch is the Exception class. This class provides a large set of built-in exception classes for automatically handling divide-by-zero errors, file I/O errors, invalid typecasts, and many other exception conditions.

Another type of group in the TObject branch are classes that encapsulate data structures, such as:

  • TBits, a class that stores an "array" of Boolean values.
  • TList, a linked list class.
  • TStack, a class that maintains a last-in first-out array of pointers.
  • TQueue, a class that maintains a first-in first-out array of pointers.

In the VCL, you can also find wrappers for external objects like TPrinter, which encapsulates the Windows printer interface, and TRegistry, a low-level wrapper for the system registry and functions that operate on the registry. These are specific to the Windows environment.
TStream is good example of another type of class in this branch. TStream is the base class type for stream objects that can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.

Overall, this branch includes many different types of classes that are very useful to you as a developer.

The TPersistent Branch

The TPersistent branch includes all VCL and CLX objects that descend from TPersistent but not from TComponent. Persistence determines what gets saved with a form file or data module and what gets loaded into the form or data module when it is retrieved from memory.
Objects in this branch implement properties for components. Properties are only loaded and saved with a form if they have an owner. The owner must be some component. This branch introduces the GetOwner function which lets you determine the owner of the property.

Objects in this branch are also the first to include a published section where properties can be automatically loaded and saved. A DefineProperties method also allows you to indicate how to load and save properties.

Following are some of the other classes in the TPersistent branch of the hierarchy:

  • TGraphicsObject, an abstract base class for graphics objects such as: TBrush, TFont, and TPen. 
  • TGraphic, an abstract base class for objects such as TBitmap and TIcon, which store and display visual images.
  • TStrings, a base class for objects that represent a list of strings.
  • TClipboard, a class that contains text or graphics that have been cut or copied from an application.
  • TCollection, TOwnedCollection, and TCollectionItem, classes that maintain indexed collections of specially defined items.

The TComponent Branch

The TComponent branch contains objects that descend from TComponent but not TControl. Objects in this branch are components that you can manipulate on forms at design time. They are persistent objects that can do the following:

  • Appear on the Component palette and can be changed in the form designer.
  • Own and manage other components.
  • Load and save themselves.

Several methods in TComponent dictate how components act during design time and what information gets saved with the component. Streaming is introduced in this branch of the VCL and CLX. C++Builder handles most streaming chores automatically. Properties are persistent if they are published and published properties are automatically streamed.

The TComponent class also introduces the concept of ownership that is propagated throughout the VCL and CLX. Two properties support ownership: Owner and Components. Every component has an Owner property that references another component as its owner. A component may own other components. In this case, all owned components are referenced in the component's Array property.

A component's constructor takes a single parameter that is used to specify the new component's owner. If the passed-in owner exists, the new component is added to the owner's Components list. Aside from using the Components list to reference owned components, this property also provides for the automatic destruction of owned components. As long as the component has an owner, it will be destroyed when the owner is destroyed. For example, since TForm is a descendant of TComponent, all components owned by the form are destroyed and their memory freed when the form is destroyed. This assumes that all of the components on the form clean themselves up properly when their destructors are called.

If a property type is a TComponent or a descendant, the streaming system creates an instance of that type when reading it in. If a property type is TPersistent but not TComponent, the streaming system uses the existing instance available through the property and read values for that instance's properties.
When creating a form file (a file used to store information about the components on the form), the form designer loops through its components array and saves all the components on the form. Each component "knows" how to write its changed properties out to a stream (in this case, a text file). Conversely, when loading the properties of components in the form file, the form designer loops through the components array and loads each component.

The types of classes you'll find in this branch include:

  • TActionList, a class that maintains a list of actions used with components and controls, such as menu items and buttons.
  • TMainMenu, a class that provides a menu bar and its accompanying drop-down menus for a form. 
  • TOpenDialog, TSaveDialog, TFontDialog, TFindDialog, TColorDialog, and so on, classes that provide commonly used dialog boxes.
  • TScreen, a class that keeps track of what forms and data modules have been instantiated by the application, the active form, and the active control within that form, the size and resolution of the screen, and the cursors and fonts available for the application to use.

Components that do not need a visual interface can be derived directly from TComponent. To make a tool such as a TTimer device, you can derive from TComponent. This type of component resides on the Component palette but performs internal functions that are accessed through code rather than appearing in the user interface at runtime.
In CLX, the TComponent branch also includes THandleComponent. This is the base class for nonvisual components that require a handle to an underlying Qt object such as dialogs and menus.

See Working with components for details on setting properties, calling methods, and working with events for components.

Add comment

Security code

Programming - Delphi