Quick start manual
Classes and objects
7-21
Properties
The definition of an array property can be followed by the default directive, in which
case the array property becomes the default property of the class. For example,
type
TStringArray = class
public
property Strings[Index: Integer]: string ...; default;
ƒ
end;
If a class has a default property, you can access that property with the abbreviation
object[index], which is equivalent to object.property[index]. For example, given the
declaration above, StringArray.Strings[7] can be abbreviated to StringArray[7]. A class
can have only one default property. Changing or hiding the default property in
descendant classes may lead to unexpected behavior, since the compiler always
binds to properties statically.
Index specifiers
Index specifiers allow several properties to share the same access method while
representing different values. An index specifier consists of the directive index
followed by an integer constant between –2147483647 and 2147483647. If a property
has an index specifier, its read and write specifiers must list methods rather than
fields. For example,
type
TRectangle = class
private
FCoordinates: array[0..3] of Longint;
function GetCoordinate(Index: Integer): Longint;
procedure SetCoordinate(Index: Integer; Value: Longint);
public
property Left: Longint index 0 read GetCoordinate write SetCoordinate;
property Top: Longint index 1 read GetCoordinate write SetCoordinate;
property Right: Longint index 2 read GetCoordinate write SetCoordinate;
property Bottom: Longint index 3 read GetCoordinate write SetCoordinate;
property Coordinates[Index: Integer]: Longint read GetCoordinate write SetCoordinate;
ƒ
end;
An access method for a property with an index specifier must take an extra value
parameter of type Integer. For a read function, it must be the last parameter; for a
write procedure, it must be the second-to-last parameter (preceding the parameter
that specifies the property value). When a program accesses the property, the
property’s integer constant is automatically passed to the access method.