Author: Jeff Guidotti
By using default array properties, you can abbreviate your calls by using the
syntax Class[I] as opposed to Class.Items[I].
Answer:
Delphi supports abbreviated calls to the default class array property. This can be
used in any delphi class that uses defaults such as TStringList.
For Example:
use MyStringList[Index] in place of MyStringList.Strings[Index]
This is somewhat useful for cleaning up the code, especially if you are accessing
the property in question often.
To add this feature to your own classes, simply add the "default" directive
(storage specifier) after the array (indexed) property you want to use as the
default.
For Example:
1 type2 TMyGraphicList = class3 public4 property Names[Index: Integer]: stringread GetName write SetName;
5 property Objects[Index: Integer]: TObject read GetObj write SetObj;
6 property Images[Index: Integer]: TImage read Get writeset; default;
7 {...}8 end;
By adding the "default" directive after the Images property we have designated the
Images array property as the default.
Access can now be abbreviated such as this:
9 for I := 0 to MyGraphicList.Count - 1 do10 begin11 AName := MyGraphicList.Names[I];
12 AObject := MyGraphicList.Objects[I];
13 AImage := MyGraphicList[I]; // instead of MyGraphicList.Images[I]14 end;
NOTE: There can be only one default array property for each class. Entering a
second would generate a compiler error.
IMPORTANT: "Array" properties are different than "attribute" properties, and the default directive takes on a different meaning for each. When used on an attribute property, the default directive (storage specifier) releates to how Delphi saves the values of published properties in form (.DFM) files.