|
|
|
|
|
Each published property can have a default value. This value is stored as a part of the RTTI information. Delphi uses a default value of the property in order to determine whether the property value should be stored in the form file (*.DFM). The property is stored in the DFM file if its value is not equal to the default only. Delphi uses this in order to save space in the form file.
The default value is applied when writing DFM - file only. An object's constructor should explicitly set the values of the object's properties to the default. See Listing 1. TEnum=(enVal1, enVal2, enVal3); Listing 1. The common error - programmer forget to initialize property in the constructor. Or may be he decides to change the default value later or to define new property... RTTI allows getting the property type and its default value. So it's easy to write generic procedure that will set all properties values to the default value defined in the declaration. This procedure should be called in the first line of the constructor to make sure that all properties are correctly initialized. See Listing 2. procedure SetPropertyDefaults(obj:TObject); Listing 2. Code explanation.The const section defines tkPropsWithDefault constant that defines a set of all possible property types that could have a default value. In the first line of code count variable is assigned to the number of object's properties using GetPropList function. A call to AllocMem functions allocates memory for the list of properties and stores a pointer in the PropList variable. A for loop iterates through the PropList array. Each property which default value is not equal to NoDefault constant is
set to its default value with SetOrdProp procedure defined in typInfo.pas as Go To Page: 1 2
The copyright of the article RTTI Part 3. Working with property's defaults. in Delphi Programming is owned by . Permission to republish RTTI Part 3. Working with property's defaults. in print or online must be granted by the author in writing.
|
|
|
|