.NET Generics for VB Programmers

In The C++ Programming Language, Bjarne Stroustrop discusses the pseudo-invention of template methods in the C programming language by employing the preprocessor and macros. Stroustrop builds on this clever use of C's preprocessor and macro capability, making templates a full-fledged part of the fledgling C with Classes (now C++). The added advantage is that templates are type checked by the compiler rather than simple text substitution by a preprocessor.

.NET 2.0 supports templates called generics. The basic concept is the same: Define a method or a class with one or more methods, specifying the data type as a replaceable element. By convention, the capital letter T is used. Finally, when you use the method or class, indicate a type for the generic T and the compiler will use that new information to generate a new, unique method or class based on that type. The new element becomes a complete and distinct block of code that benefits from being processed by the compiler.

This article shows how to define generic methods and classes and use the generic classes that ship with .NET 2.0.

Defining Generic Methods

The generics capability of .NET 2.0 can be defined at the granularity of a single method. The key is to separate data type from algorithm and parameterize the data type. Each reference to the method with a distinct data type yields a distinct method. Generic methods also support constraints and overloading. A generic method constraint is a new language feature that adds a type constraint to the generic type; this predicate restricts the data type of the generic parameter. Consequently, methods can be overloaded based on the presence and absence of generic parameters. (An upcoming section reviews a few examples of overloaded generic and non-generic methods.)

The classic use of generics is separating data types from common algorithms, the canonical example being turning sort algorithms into generic sorting algorithms. A straightforward example is parameterizing a Swap method. Listing 1 demonstrates how you can define a generic Swap method that swaps any two reference arguments. It includes a console application that demonstrates how to invoke the generic method:

Note: In this instance, you also could use an object data type for the Swap method because all .NET types share a common base type.

The new elements needed to define a generic method are the parentheses, the Of keyword, and an argument representing the generic type, as demonstrated by the (Of T) characters after the method name Swap. Then, everywhere the generic type is used, use the parameterized argument (in this example, T).

Swap is a generic method that has two replaceable arguments and one replaceable local variable. For example, Swap(Of Integer) effectively results in a Swap method with two Integer parameters and a temp variable defined as an Integer.

Adding a Generic Method Constraint

Suppose you want to constrain types in Swap (in Listing 1) to non-nullable structure types. You could add a constraint to Swap to indicate that Swap is applicable only to value types (structures). Listing 2 shows Swap defined as being constrained to structures (or value types). The result is that Swap(Of String) in Listing 1 will no longer work:

Listing 2: Swap with a Constraint Limiting the Swap Method to Value Types

Public Sub Swap(Of T As Structure)(ByRef a As T, ByRef b As T)
Dim temp As T
temp = a
a = b
b = temp
End Sub

Parameterized types can be limited to structures, classes, base classes, interfaces, and types that have a default constructor (Sub New with no parameters). The As predicate in bold in Listing 2 demonstrates how to constrain the parameterized type.

Generics support defining multiple parameterized types, and each parameterized type can have no or multiple constraints.

Overloading Generic Methods

Methods can be overloaded based on arguments but not return types, and methods can be overloaded by parameterized types too. For example, all of the following can exist in the same scope:

.NET Generics for VB Programmers

Defining a Generic Class

Generic classes are defined in the same instances that regular classes are defined, with one difference. A regular class is defined when you have data and more then one method that work as a cohesive unit or solution. Generic classes are defined when methods and data work as a cohesive unit and it is possible to abstract the data in such a way that multiple data types could be supported with the same code. For example, queues, lists, and stacks do not care what they store, just how they store it. If you use a queue, stack, or list of objects, you have to perform messy type conversions all over your code. If you use a generic queue, stack, or list, the type conversion occurs internally to the class. That is to say, the locus for the messy type conversions converge to an internal point in the class, and the class consumer can rely on compiler-checked types and isn't required to perform if-conditional checks and type conversions.

Defining a generic class is like defining multiple generic methods, with one addition: the (Of T) construct is used in the class header too. To demonstrate, you have a defined a generic strongly typed collection (see Listing 3) derived from System.Collections.CollectionBase. You can use this one class for any data type, as if you had defined a custom typed collection for all types:

Listing 3: A Generic Strongly Typed Collection

Module Module1
Sub Main()
Dim BrokenBones As TypedCollection(Of OrthoInjury) = _
New TypedCollection(Of OrthoInjury)
BrokenBones.Add(New OrthoInjury(True, _
"Broken Right Clavicle", "Vicodin; Heals n 8 to 12 weeks"))
BrokenBones.Add(New OrthoInjury(True, _
"Fractured Posterior Rib #5", "Heals in 6 to 8 weeks"))
BrokenBones.Add(New OrthoInjury(True, _
"Fractured Posterior Rib #1", "Heals in 6 to 8 weeks"))
Dim injury As OrthoInjury
For Each injury In BrokenBones
Console.WriteLine("Description: " & injury.Description)
Next
Console.ReadLine()
End Sub
End Module
Public Class TypedCollection(Of T)
Inherits System.Collections.CollectionBase
Default Public Property Item(ByVal Index As Integer) As T
Get
Return CType(List(Index), T)
End Get
Set(ByVal value As T)
List(Index) = value
End Set
End Property
Public Function Add(ByVal value As T) As Integer
Return List.Add(value)
End Function
End Class
Public Class OrthoInjury
Private FHasXray As Boolean
Private FDescription As String
Private FPrognosis As String
Public Sub New(ByVal HasXray As Boolean, _
ByVal Description As String, ByVal Prognosis As String)
FHasXray = HasXray
FDescription = Description
FPrognosis = Prognosis
End Sub
Public Property HasXray() As Boolean
Get
Return FHasXray
End Get
Set(ByVal value As Boolean)
FHasXray = value
End Set
End Property
Public Property Description() As String
Get
Return FDescription
End Get
Set(ByVal value As String)
FDescription = value
End Set
End Property
Public Property Prognosis() As String
Get
Return FPrognosis
End Get
Set(ByVal value As String)
FPrognosis = value
End Set
End Property
End Class

If you have read previous articles on typed collections, you will see that the generic typed collection (in bold in Listing 3) is basically a strongly typed collection with the data type parameterized.

Using Predefined Generic Classes

Fortunately, you are not required to define generic classes from scratch. The System.Collections.Generic namespace defines many of the classic data structures, such as List, Queue, and Stack, as generics already. You simply have to import that namespace and declare an instance of the type you want. For example, the following code adequately replaces your custom typed collection with the .NET 2.0 generic List typed collection:

As a general rule, if you want to store more than one type (heterogeneous types), use the older style classes like Queue and Stack. If you want to use just one type (homogeneous types), use the newer generic classes in the System.Collections.Generic namespace. Generally, you'll want the newer generic classes.

The Choice to Learn

Fewer and fewer things are separating traditionally complex languages like C++ from traditionally simpler languages like VB. At first glance, this fact can be frustrating because it implies VB is now harder to learn—like C++ is. In reality, the core language (VB) is pretty much the same, and just like C++, you always have the choice of learning more advanced idioms like generics if and when you need them. Now, you aren't handicapped by absence of choice.

Also, keep in mind that you always have the option of learning any programming idiom as a consumer first—use what's there like generic Lists—and then as a producer, learning how to create your own. Trying to master everything at once can be overwhelming and just isn't necessary.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.

If you are interested in joining, sponsoring a meeting, or posting a job, check out www.glugnet.org, the Web page of the Greater Lansing area Users Group for .NET.

Comments

There are no comments yet. Be the first to comment!

You must have javascript enabled in order to post comments.

Leave a Comment

Your email address will not be published. All fields are required.

Name

Email

Title

Comment

Top White Papers and Webcasts

Many businesses still rely on a legacy telephony infrastructure that is costly and complicated in the mistaken belief that it is more expensive and disruptive to change. These businesses are often slow to adopt new communications platforms that can provide a competitive advantage, decrease costs, and grow with the business. Answer a few simple questions about your organization and get a personalized paper that explores the benefits of cloud-based communications platforms.

Today, users, applications, and data exist in more places than ever before, creating an unprecedented challenge for IT. How can IT achieve the flexibility and agility it needs to offer multiple types of applications in multiple locations? To better serve business demands for information everywhere, enterprises must develop new strategies for optimizing multiple kinds of networks. Read this white paper to learn how hybrid networks provide an unprecedented level of network dynamism, enterprise agility, and the …