2.14. Create a Generic Type

Problem

You need to create a new general-purpose type such as a collection or container that supports strong typing of the elements it contains.

Solution

Define your class using the generics syntax, first introduced in .NET Framework 2.0, provided in VB .NET 9.0.

How It Works

You can leverage the generics capabilities of VB .NET 9.0 in any class you define. This allows you to create general-purpose classes that can be used as type-safe instances by other programmers. When you declare your type, you identify it as a generic type by following the type name with a list of identifiers for the types used in the class, preceded by the Of keyword and enclosed in parentheses. Here is an example:

Public Class MyGeneric(Of ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.