Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associative Arrays 4

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hi guys,

Just wondering if anyone knows a good way to implement an associative array in delphi. The only way I can see is to create a record, then create and array of that, i.e.

Code:
type
  TData = record
    String1:String;
    Int1:Integer;
  end;

var
  DataArray:Array of TData;

Can anyone think of a more efficient way of achieving this?

Thanks.
 
The only thing I can think of is if you can make the numbers (int1) directly relational to the array spots. Beyond that, it looks like you have what you need there.

A lot really depends on what data is in the array, and how you intend to use it
 
Thanks, nice to know that I am correct.

I mainly program in PHP these days, and PHP makes life very easy for stuff like this.
 
The TStringList class provides a good basis for implementing an associative array in Delphi. TStringList already has your equivalent of String1 (the strings property) and Int1 (the objects property).

If you use the TDataset component in your database applications you will have probably come across accessing fields by using FieldByName which is, in effect, an associative array. Delphi implements this by using TStringList.

If you have Delphi 6 or later you can use TBucketList and TObjectBucketList which actually implements an associative array for you.

TBucketList has a ForEach procedure which calls a procedure for each item in the associative array which is quite nice.



Andrew
Hampshire, UK
 
Me neither. Thanks Andrew!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top