Consuming Generic Types

To instantiate and consume generic types, you pass to the constructor the type you want to be handled. For example, the following code creates a new instance of the CustomType class enabling it to handle only integers or types that are converted to Integer via a widening conversion:

image

Basically you pass the desired type to the constructor after the Of keyword. When invoking the Add method, you can notice how IntelliSense tells you that the method can receive only Integer. If you pass a different type, you get an error message. But this does not work only with .NET common types. You can use this technique with custom types, ...

Get Visual Basic® 2010 Unleashed 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.