6.7.3 Array properties

Array properties also exist. These are properties that accept an index, just as an array does. The index can be one-dimensional, or multi-dimensional. In difference with normal (static or dynamic) arrays, the index of an array property doesn’t have to be an ordinal type, but can be any type.

A read specifier for an array property is the name method function that has the same return type as the property type. The function must accept as a sole argument a variable of the same type as the index type. For an array property, one cannot specify fields as read specifiers.

A write specifier for an array property is the name of a method procedure that accepts two arguments: the first argument has the same type as the index, and the second argument is a parameter of the same type as the property type. As an example, see the following declaration:

Type  
  TIntList = Class  
  Private  
    Function GetInt (I : Longint) : longint;  
    Function GetAsString (A : String) : String;  
    Procedure SetInt (I : Longint; Value : Longint;);  
    Procedure SetAsString (A : String; Value : String);  
  Public  
    Property Items [i : Longint] : Longint Read GetInt  
                                           Write SetInt;  
    Property StrItems [S : String] : String Read GetAsString  
                                            Write SetAsstring;  
  end;  
 
Var  
  AIntList : TIntList;

Then the following statements would be valid:

AIntList.Items[26] := 1;  
AIntList.StrItems[’twenty-five’] := ’zero’;  
WriteLn (’Item 26 : ’,AIntList.Items[26]);  
WriteLn (’Item 25 : ’,AIntList.StrItems[’twenty-five’]);

While the following statements would generate errors:

AIntList.Items[’twenty-five’] := 1;  
AIntList.StrItems[26] := ’zero’;

Because the index types are wrong.

Array properties can be multi-dimensional:

Type  
  TGrid = Class  
  Private  
    Function GetCell (I,J : Longint) : String;  
    Procedure SetCell (I,J : Longint; Value : String);  
  Public  
    Property Cellcs [Row,Col : Longint] : String Read GetCell  
                                            Write SetCell;  
  end;

If there are N dimensions, then the types of the first N arguments of the getter and setter must correspond to the types of the N index specifiers in the array property definition.