Thursday, September 25, 2008

A simple Generic List: TList<T>

Delphi 2009 has support for generics, and has 'built in' generic types like TList, TArrays etc. If you want to use them you must add Generics.Collections to your uses clause.

Generic List: TList
Suppose we want a list with persons representing the TPerson class:

// Our class TPerson
= class
FFirstName : string;
FLastName : string;
function GetFullName: string;
constructor Create(ALastName : String;
AFirstName : string);
property FirstName : string read FFirstName
write FFirstName;
property LastName : string read FLastName
write FLastName;
property FullName : string read GetFullName;

//Define the generic list
PersonsList : TList<TPerson>;

//Method to fill the list
procedure TForm4.btnPopulateClick(Sender: TObject);
//Filll the personslist
'Anon', 'Method'));

//Traveling this list to fill a TListBox
procedure TForm4.FillListBox;
p : TPerson;
for p in PersonsList do begin

As you can see, no rocking science needed to implement a generic list!

Sorting a generic TList
The next step is to implement a sorting method to sort the list on, for example, the Lastname property.

A TList has a Sort property which has an IComparer interface that can be implemented like this using an anonymous method:

(You will have to add generics.default to your uses clause)
procedure TForm4.btnSortClick(Sender: TObject);
function(const Item1,Item2:TPerson): Integer
Result :
CompareText(Item1.LastName, Item2.LastName);

Generics is a great new feature of Delphi 2009. Just as in C# you will use them all the time. In this context, the use of anonymous methods is great.

Besides TList Delphi has more standard generics typs including TArray, TEnumarable and more, so there is always more to explore!

Update 25-09-2008 10:58
As animal commented you must of course create the list first, which I did not include in the code snippets.

PersonsList:= TList.Create;

And you will have to free it also at sometime. (Don't forget to free the TPerson objects as well!)

for p in PersonsList do begin;


Oliver said...

I guess you left out the construction/deconstruction for TList<TPerson> for briefness only, didn't you?

Olaf Monien said...

There are two things that should be mentioned in that context:

- the non-generic versions of all these container classes still exist (in Contnrs.pas), in other words the non-generic and generic classes are "parallel" implementations.

- the generic version of TList is of course no longer pointer based, but the fully object aware predecessor of TObjectList.

Roland Beenhakker said...

Of course you are right that you will have to create the TList and free it.
Added it to the post.

Anonymous said...

So is there TObjectList{T} or something similar to handle object lifetime?

Bruce McGee said...



TList{T}, TQueue{T}, TStack{T} and TDictionary{TKey, TValue} all have object counterparts to manage lifetimes. e.g. TObjectList{T}

Anonymous said...

And what about Thread safety and ThreadLists, InterfaceLists etc?

Just thought that TList{T} should work ok with interfaces!?

Olaf Monien said...

no, there is no generic counterpart for TThreadList.

Anonymous said...

Great article, that's the first useful use of anonymous methods in Delphi I've seen.

Anonymous said...

Great article!
But D2009 IDE shows the code hint:
PersonsList.Add[]; // square bracket, not parenthesis.
This is a low-level bug.

Anonymous said...

In update 25-09-2008, where you say:

PersonsList:= TList.Create; probably meant:

PersonsList:= TList<TPerson>.create

But your blog software removed what it thought was an unrecognized html tag.

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...