O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create a Strongly Typed Dictionary Collection

Problem

You need to create a key-based collection that can only contain one type of object and rejects attempts to add other data types.

Solution

Create a custom dictionary collection class by deriving from System. Collections.DictionaryBase, and implement type-safe Add, Remove, and Item methods.

Discussion

With a custom dictionary collection, you can place restrictions on the type of objects stored in the collection and on the type of object used to index items in the collection. Internally, the DictionaryBase object uses an ordinary weakly typed Hashtable, which is exposed through the protected property Dictionary. However, external code can’t directly access this collection. Instead, it must use one ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required