Default Property Values

You might have noticed that the values of the Bool and Enumeration properties are always displayed boldfaced, which means that the values of these properties are modified and that they will eventually be saved to the .dfm file.

These properties are displayed as modified in the Object Inspector because we haven't marked them with the default directive. The default directive is a storage specifier that allows us to tell Delphi which value is the default value. When saving components to the .dfm file, Delphi uses the default value to see whether or not it should save the property's value to the .dfm file. If the property's value differs from the default value, it will be saved to the .dfm file. If the current property value matches the default value, Delphi will not save the property to the .dfm file, thus reducing the size of both the .dfm file and the final executable (especially if you have a large number of components with non-default property values).

To define a default value for a property, simply add the default directive to the end of the property declaration, followed, of course, by an appropriate constant. The following listing shows how to set default values for the Bool and Enumeration properties of the TSimple component.

Listing 24-5: Default property values

image from book
property Bool: Boolean read FBool write FBool default False; property Enumeration: TDaysEnum read FEnumeration   write FEnumeration default deMonday;
image from book

Currently, the default directive will only work properly if you specify the default type values that are automatically assigned by Delphi to all properties — 0 for numbers, empty string for strings, nil for pointers, the first enumeration value for enumerations, etc.

To specify a default value that differs from the automatically assigned property value, you have to change the property's value in the component's constructor (you'll see how to do this shortly).

The following figure shows the .dfm file and the Object Inspector without the default directive and after the default directive has been added to the Bool and Enumeration properties.

image from book
Figure 24-12: The result of the default directive

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: