Search code examples
delphidynamic-arrays

Delphi dynamic array working like ListItem in a ListView


I like ListViews. In fact, I will go so much as to say that I love ListViews. So easy to use, especially with adding new items, deleting items, counting items, and running through all of the items.

I would like to create a dynamic array that I can manage in pretty much the same way.

Something like this:

  • You add new items using code like ListItem := ListView1.Items.Add;
  • You can reference items similar to ListView1.Items.Item[i]
  • You can remove items using code like ListItem.Free (or ListView1.Items.Item[i].Free), and it automatically re-indexes the remaining items
  • You can count the items like ListView1.Count

Does anybody have a "template" of a non visual component that will give the same kind of functionality for adding items, removing items and counting items within the dynamic aray?

Or, are there non-visual components which exist which I could inherit to give me the functionality I seek?


Solution

  • Take a look at Generics.Collections unit. It consists of a number of classes you are looking for. Especially, TObjectList.