Creating a Strongly-Typed Dictionary

As with the Collection, it is likely that you might need a strongly-typed Dictionary. In the HashTable example presented in this chapter, the data stored could have been any type descending from System.Object. The following section illustrates how to create a Dictionary that enforces the TStateInfo type.

Descending from DictionaryBase

The System.Collections namespace defines the DictionaryBase class. From this class, you should derive your strongly-typed dictionary classes. Listing 10.7 shows such a class.

Listing 10.7. Strongly-Typed Dictionary
1: unit d4dnDevGuide.StateDictionary; 2: interface 3: 4: uses 5: System.Collections; 6: 7: type 8: 9: // Define a class to store in the dictionary 10: TStateInfo = ...

Get Delphi for .NET Developer’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.