1.Property Name : Property-type Index Constant Read Getter Write Setter;
2.Property Name[Index : Index type] : Property-type Read Getter Write Setter;
3.Function|Procedure Index Constant;
4.Function|Procedure header; External DllName Index Constant;
Description
The Index directive is principally used to declare indexed class data properties.
These principally work against array or list types of data.
Version 1
Retrieves or writes an indexed value by specifying a Constant index number.
Version 2
Retrieves or writes an indexed value by specifying an index parameter. As can be seen, this does not actually use the Index directive - it is included here for completeness.
See the example or the Property keyword for information about coding the routines associated with these properties.
Version 3
Defines a unique index for routine when exporting.
Version 4
Defines the unique index of an external routine being imported.
Related commands
Function Defines a subroutine that returns a value
Procedure Defines a subroutine that does not return a value
Property Defines controlled access to class fields
Example code : Illustrate both varieties of Indexed properties
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
// Class with Indexed properties
TRectangle = class
private
fCoords: array[0..3] of Longint;
function GetCoord(Index: Integer): Longint;
procedure SetCoord(Index: Integer; Value: Longint);
public
property Left : Longint Index 0 read GetCoord write SetCoord;
property Top : Longint Index 1 read GetCoord write SetCoord;
property Right : Longint Index 2 read GetCoord write SetCoord;
property Bottom : Longint Index 3 read GetCoord write SetCoord;
property Coords[Index: Integer] : Longint read GetCoord write SetCoord;
end;
// The form class itself
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// TRectangle property 'Getter' routine
function TRectangle.GetCoord(Index: Integer): Longint;
begin
// Only allow valid index values
if (Index >= 0) and (Index <= 3)
then Result := fCoords[Index]
else Result := -1;
end;
// TRectangle property 'Setter' routine
procedure TRectangle.SetCoord(Index, Value: Integer);
begin
// Only allow valid index values
if (Index >= 0) and (Index <= 3)
then fCoords[Index] := Value;
end;
// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
myRect : TRectangle;
i : Integer;
begin
// Create my little rectangle
myRect := TRectangle.Create;
// And set the corner coordinates
myRect.Left := 22; // Left using direct method
myRect.Top := 33;
myRect.SetCoord(2,44); // Right using indexed method
myRect.SetCoord(3,55);
// And ask for these values
for i:= 0 to 3 do
ShowMessage('myRect coord '+IntToStr(i)+' = '+intToStr(myRect.GetCoord(i)));
end;
end.
myRect coord 0 = 22
myRect coord 1 = 33
myRect coord 2 = 44
myRect coord 3 = 55