Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:00:07+00:00 2026-05-30T10:00:07+00:00

Arrays can be indexed using user-defined enumerated types. For example: type TIndexValue = (ZERO

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:00:08+00:00Added an answer on May 30, 2026 at 10:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Cobol's SEARCH keyword can be used to search one dimensional arrays indexed. But
I know two arrays can be zipped and the result can be iterated with
How can i combine two arrays in to a single array during compound selection
Using two databases to illustrate this example: CouchDB and Cassandra . CouchDB CouchDB uses
A PHP array can have arrays for its elements. And those arrays can have
You can use arrays with str_replace(): $array_from = array ('from1', 'from2'); $array_to = array
Since arrays and hashes can only contain scalars in Perl, why do you have
I'm trying to create a class that can instantiate arrays at runtime by giving
How can I store arrays in single array? e.g. I have four different arrays,
If we can use pointers and malloc to create and use arrays, why does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.