Search code examples
delphitypesenumststringlisttlist

Delphi: Types other than Integer for indexing TStringList items


Arrays can be indexed using user-defined enumerated types. For example:

type
  TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);

var
  MyArray: array[Low(TIndexValue) .. High(TIndexValue)] of String;

Elements from this array can then be referenced using TIndexValue values as an index:

MyArray[ZERO] := 'abc';

I am trying to obtain this same general functionality with a TStringList.

One simple solution is to cast every index value to an Integer type at the time of reference:

MyStringList[Integer(ZERO)] := 'abc';

Another solution (to hide all the casting) is to create a subclass of TStringList and defer all the casting to this subclass's subroutines that access the inherited Strings property:

type
  TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);

type
  TEIStringList = class(TStringList)
  private
    function GetString(ItemIndex: TIndexValue): String;
    procedure SetString(ItemIndex: TIndexValue; ItemValue: String);
  public
    property Strings[ItemIndex: TIndexValue]: String 
      read GetString write SetString; default;
  end;

function TEIStringList.GetString(ItemIndex: TIndexValue): String;
begin
  Result := inherited Strings[Integer(ItemIndex)];
end;

procedure TEIStringList.SetString(ItemIndex: TIndexValue; ItemValue: String);
begin
  inherited Strings[Integer(ItemIndex)] := ItemValue;
end;

This works fine for a single implementation that uses the enumerated type TIndexValue.

However, I would like to re-use this same logic or subclass for several different TStringList objects that are indexed by different enumerated types, without having to define TStringList subclasses for each possible enumerated type.

Is something like this possible? I suspect I may have to depend on Delphi's Generics, but I would be very interested to learn that there are simpler ways to achieve this.


Solution

  • I think that generics would be by far the most elegant solution. Using them would be as simple as rewriting your class above as:

    TEIStringList<T> = class(TStringList) 
    

    and then replacing all TIndexValue references with T. Then you could create it just as any other generic:

    var
      SL: TEIStringList<TIndexValue>;
    begin
      SL:=TEIStringList<TIndexValue>.Create;
      (...)
      ShowMessage(SL[ZERO])
      (...)
    end;
    

    If you insist on avoiding generics, maybe operator overloading would be of use. Something like the following should work:

    type
      TIndexValueHolder = record
        Value : TIndexValue;
        class operator Implicit(A: TMyRecord): integer;
      end;
    
    (...)
    
    class operator TIndexValueHolder.Implicit(A: TMyRecord): integer;
    begin
      Result:=Integer(A);
    end;
    

    Then use with:

    var
      Inx : TIndexValueHolder;
    
    begin
      Inx.Value:=ZERO;
      ShowMessage(SL[Inx]);
    end
    

    UPDATE: You could adapt TIndexValueHolder for use in a for or while loop by adding Next, HasNext, etc. methods. This might end defeating the purpose, though. I'm still not sure what the purpose is, or why this would be useful, but here's some ideas for how to do it, anyways.