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 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.