6.4.3 Array properties

Array properties also exist. These are properties that accept an index, just as an array does. Only now the index 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 arguent 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.