Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

August Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Det...   Print This     
Part of the mo. lesson in the Jul 08 Issue of Prestwood eMag
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
Associative Arrays in Delphi/Object Pascal (Use TStringList)

TStringList Example
Object Pascal doesn't have a native associative array, but you can use a TStringList the same way. (Alternatively, search the Internet for TStringHash and THashedStringList classes for implementations of a true associative array).

  StateList : TStringList;
  StateList := TStringList.Create; //Create list.
  //Add values comma separated.
  StateList.CommaText := 'CA=California, AR=Arizona';
  StateList.CommaText := StateList.CommaText + ', FL=Florida';
  //Also, can add individually.
  //Use it.
  ShowMessage('FL is ' + StateList.Values['FL']);

More Info

Code:  Assocative Arrays in ObjectPAL
Definition:  Associative Array
FAQ:  Associative Arrays in ASP Classic
Tip:  Associative Arrays in Delphi/Object Pascal
Code:  Associative Arrays in JavaScript


Share a thought or comment...
First Comment
Comment 1 of 9

Delphi's TStringList is a very powerful class, as is its ancestor, TStrings. There is, however, one "gotcha:" A very large StringList can bog down performance if its Sorted property is True while you add items.  See this tidbit for a tip on maximizing StringList performance.

In addition to name/value pairs, TStrings offers another item member, Object.  Object is a pointer to which you can assign just about anything.  One handy trick is to cast a data table's row's integer primary key to a pointer, and store it in your TStrings.Objects.  Later, you can cast the Object[i] pointer back to an integer to retrieve the PK. This is very useful in ListBoxes, ComboBoxes, etc.; any component with a TStrings member.

Posted 12 years ago

Comment 2 of 9

Another Delphi candidate to stand in for an associative array is TObjectList. It holds a list of pointers to any object you care to create.  If you have a class with multiple members, you can associate many values - of many different types - in your class instances, then put them in a TObjectList.

One important Boolean propety of TObjectList is OwnsObjects.  If set to True, your TObjectList instance will free the objects it stores when it is freed, as well as upon some other operations (see Delphi help for details). If False, it's up to you to free all the objects - probably before freeing the ObjectList.

The downside (an upside in some cases) to TObjectList is that it has no predefined sorting mechanism, which makes sense, because it really has no idea what it contains.  You'll have to write your own "Compare," a TListSortCompare function.  Again, see Delphi help.

Posted 12 years ago

Comment 3 of 9

Thanks Wes for adding to this code snippet. Mucho appreciated!

Posted 12 years ago

Comment 4 of 9

Newbie posting, so please revise or move if desired:

What Wes was referring to about freeing objects within a list manually is shown below using a combo box, for example, as a list type item holding objects within itself.

class procedure TUtils.FreeAndNilComboBoxObjects(p_cboWork: TComboBox);
  Pass in the combo box, and this will clear any objects
  within the combo box
  nCount: integer;
  for nCount := 0 to (p_cboWork.Items.Count - 1) do
    if (p_cboWork.Items.Objects[nCount] <> nil) then
      p_cboWork.Items.Objects[nCount] := nil;
String Grids can hold objects too, here is sample code
  for nCount := 0 to (p_sgdWork.RowCount - 1) do
    if (p_sgdWork.objects[0,nCount] <> nil) then
      p_sgdWork.objects[0,nCount] := nil;
Posted 12 years ago

Comment 5 of 9

Hi Daniel:

I just wanted to welcome you and say your post is just fine right here. We welcome your input.

Posted 12 years ago

Comment 6 of 9

Such services as masterpapers can help you to achieve the best result with your papers written and formatted with high quality. It will be a good decision. 

Posted 6 months ago

Comment 7 of 9

You are the best blogger i ever seen your content is truly a genuine content that inspire many peoples i humble request you to make it more better Escorts Service in Bangalore

Posted 6 months ago

Comment 8 of 9

You will thank me if you are a student because I know how hard it is. I just remember I had to do huge homework including essays which I hated the most. So if you don‘t like to do that too, then ask guys from paramountessays to help you. You will simplify your life so much, I promice!

Posted 6 months ago

Latest Comment
Comment 9 of 9

You can’t go around building a better world for people. Only people can build a better world for people. Otherwise it’s just a cage. There isn’t a way things should be. There’s just what happens, and what we do.  cookie clicker

Posted 6 months ago
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1124A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101201 Counter
Since 6/18/2008
Sales Website: Or visit our legacy sales site:

©1995-2020 Prestwood IT Solutions.   [Security & Privacy]