Book Cover

Chapter 5
Dynamic Arrays: The TList Class


In This Chapter

Delphi often uses the TList class to hold lists of objects. The TList class is most often used when you need an array whose size can change at run time. The documentation is not always clear, so take a look at this workhorse class.

Dynamic Arrays with TList

Use the TList class for dynamic arrays. Learn how TList really works, which is different from what the documentation claims.

TList Overview

You can find documentation for all the methods and properties of the TList class in Delphi's online help files. The help files are not always accurate, so this section presents an overview of the TList class.

Using the TList Class

There are several important points to remember about the TList class, as discussed in this section. Most importantly, TList manages its own memory, but does not dereference, allocate, or free memory for the item values.

Limitations of the TList Class

The biggest problem with the TList class arises when you want to write software that is portable between Delphi 1.0 and Delphi 2.0. In Delphi 2.0, there are no limits on the number of items a list can hold, but in Delphi 1.0, a list is limited to at most 16,380 items. Another limit is the difficulty in deriving new list classes from TList. This problem lies in the nature of how TList was written, with static methods instead of virtual methods. This section discusses these problems and ways of copying with them.

A Better TList Class

After learning about the limitations of TList, consider a different class without those restrictions. The TList replacement is portable between Delphi 1.0 and Delphi 2.0 and offers greater flexibility for deriving new list classes.

Summary



Copyright © 1996 Waite Group Press