RTTI part 4. Using RTTI with properties


© Lyapin Ilya

This article continues the review of RTTI possibilities. We'll learn the generic way of changing any property of any control on a form. Using RTTI allows taking no care of the control's type.

You may treat objects that descend from different classes similarly. The 'name' property of any component that descends from TComponent can be accessed using a TComponent reference. Any component that descends from TControl (a control) has height and width properties. So you can write the code like following:

For i:=0 to ComponentCount-1 do
ComboBox1.Items.add(Components[i].Name);

Or

For i:=0 to ComponentCount-1 do
If (Components[i] is TControl) then
(Components[i] as TControl).Width:= (Components[i] as TControl).Width div 2;

However, it's possible only when the property being accessed is visible in the shared ancestor class. When two or more objects have the same property, but the property is not declared public or published in a common ancestor, it can not be accessed using a reference to an ancestor.

An example of a property like this is Color. The color property of both TLabel and TEdit is inherited from TControl. However, this property is declared protected in TControl and re-declared as published in the TEdit and TLabel.
So this code will generate compiler error:

For i:=0 to ControlsCount-1 do
Controls[i].Color:=clRed;

The decision is to cast controls[i] to proper type:

For i:=0 to ControlsCount-1 do
If (Controls[i]is TEdit) then
TEdit(Controls[i]).Color:=clRed
else If (Controls[i]is TLabel) then
TLabel(Controls[i]).Color:=clRed
and so on...

Let's look to more elegant way.

Through the RTTI you can access property in you know its name, even if objects do not have a common ancestor at all. The RTTI provides a series of functions with names like SetOrdProp, GetOrdProp, SetStrProp,GetStrProp and so forth.

These methods require a PPropInfo reference to property. You should already know how to get it (see previous article). The GetXXX functions return value of the property, the SetXXX functions receive a new values as second parameter. The use of these functions is demonstrated in the example.

Code example.

Figure 1 shows the main form of this project as it might appear while running. Listing 1 contains a source.

Figure 1.

unit Unit1;
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;

type
TForm1 = class(TForm)
Label2: TLabel;
ComboBox1: TComboBox;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
ColorDialog1: TColorDialog;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetObjectColor(objInstance:TObject;var color:integer):Boolean;
procedure SetObjectColor(objInstance:TObject;color:integer);
public
{ Public declarations }
end;

       

Go To Page: 1 2


The copyright of the article RTTI part 4. Using RTTI with properties in Delphi Programming is owned by . Permission to republish RTTI part 4. Using RTTI with properties in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo