Index Properties
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
Indexers, also known as Array Properties, are properties that can be treated like arrays:
property Items[x: Integer]: String read GetItems write SetItems; method GetItems(x: Integer): String; method SetItems(x: Integer, s: String);
The two methods above are passed the required index and how they obtain/set the correct value is very open ended. Although they might reference an array, they certainly don't have to - could be a list or a database, you name it.
Array properties can be overloaded. One restriction is that they cannot use the same name as a non-array property:
// these are OK property Test[i: Integer]: String read ... write ... property Test[s: String]: String read ... write ... property Test[s: String; I: Integer]: String ... // but these are not property Test[i: Integer]: String read ... write ... property Test: String;
Inline Readers and Writers
As with standard properties, inline code can be used to define the reader and writer for the property. The reader must be a single statement returning the proper type, the writer must be an expression that can be used as a left-hand expression in assignment of the proper type. In both parts, the indexer variable can be used. For example:
property Test[i: Integer]: String read fList[i+1] write fList[i+1];
Default Indexers
property Items[x: Integer]: String ...; default;
Supplying the default attribute to an array index tells the compiler the implied property to be used if you do not supply one. Consider the following code:
x = class method GetItems(i: Integer): Integer; property Items[i: Integer]: Integer read GetItems ; default; end;
Instead of referring to x.Items[5], you can now use the simpler x[5].
Multiple definitions of indexed properties (implicit overloading) are supported (with different array parameter types, of course) and so the following is valid also:
x = class method GetItems(i: Integer): String; method GetItems(s: String): String; property Items[i: Integer]: String read GetItems ; default; property Items[s: String]: String read GetItems ; default; end;
Now x[5] and x['5'] can be used.
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To