Search code examples
delphidelphi-7

How to use property with string index?


I have this code:

type
  TMyClass = class
    private
      procedure SetKeyValue(const Key: WideString; Value: Widestring);
      function GetKeyValue(const Key: WideString): WideString;
    public
      // this works
      property KeyValue[const Index: WideString] : WideString read GetKeyValue write SetKeyValue;

      // this does not compile
      // [Error]: Incompatible types: 'String' and 'Integer'
      property Speed: WideString index 'SPEED' read GetKeyValue write SetKeyValue;
  end;

The Speed property gives me error:

Incompatible types: 'String' and 'Integer'

I need the index to be string. Is it possible to use the index with string value?


Solution

  • That is not possible. Indexed properties support Integer as index constants only.

    See the documentation (own emphasis):

    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.