You are here:About>Computing & Technology>Visual Basic> Learn VB.NET> Visual Basic .NET 2008 Express - What's New With Visual Basic .NET Express
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Visual Basic .NET 2008 Express - What's New With Visual Basic .NET Express

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Jan 12 2008

Generics ... Another New VB Express Feature

The new feature called Generics has been added to the fundamental VB.NET syntax for all editions, including VB Express. Since it's time we did some more coding, this segment of the tutorial uses this latest feature of VB.NET 2008 to show you some.

Microsoft defines generics this way:

"A generic type is a single programming element that adapts to perform the same functionality for a variety of data types. When you define a generic class or procedure, you do not have to define a separate version for each data type for which you might want to perform that functionality."

Generics help you write classes (or, use classes that have already been written) for more purposes and avoid having to write essentially the same code in different parts of a program by leaving the "datatype" undefined. (String and Integer are examples of datatypes.)

Here's a real world analogy. If you have ever told your teenager, "Fer cryin' out loud, pick up the dirty clothes in your room!" and discovered later that "dirty clothes" was the only thing that got picked up, then you may understand why we need generics. What you meant was, "pick up the dirty clothes and the old candy bar wrappers and the CD's and ... " In other words, you wanted the "pick up" class to be used for more than one "datatype" of stuff on the floor.

Where Generics Is Used

The easiest way to start learning about generics is to use one of the generic datatypes that have been added to VB.NET. Starting with the basics, the thing to look for that tells you that you're dealing with generic types is the Of keyword. It can be present in a lot of VB Express statements. Again, don't worry if the examples below don't mean too much to you now. I'm just showing you what generics look like in code. We'll see an actual code example at the end of this article.

In a Class statement:

Public Class myAVBClass(Of T)
   Dim myVal As T
End Class

In a Structure statement:

Public Structure myAVBStruct(Of T)
   Dim myVal As T
End Structure

In a Sub:

Public Sub callTestSub()
   testSub(Of String)("A String")
   testSub(Of Integer)(5)
End Sub

Public Sub testSub(Of T)(ByVal arg As T)
   Dim a As T
   a = arg
   MessageBox.Show(a.ToString)
End Sub

You can also use generics in Delegates, Functions, and Interfaces.

The examples above all use the variable T as the placeholder for the generic datatype. The use of T isn't required (the last example here doesn't use it), but it's traditional and you see it a lot in Microsoft documentation. You could use any variable name you like but if you use T, your code will be easier to understand.

A Generics Code Example

The easiest way to introduce generics in an actual code example is to use one of the generic types that have been added to the System.Collections.Generic namespace. These are types that accept generic placeholders (like Of T) as arguments. It's worth checking out this namespace in the VB.NET Object Browser.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The traditional example, used by Microsoft in their seminars, is to start with a Framework 1.1 - the version used in VB.NET 2003 where generics aren't available - ArrayList object for comparison. (An ArrayList is a whole new kind of dynamic array that you can use in .NET.) The following code shows that you can add any datatype to the ArrayList.

Dim myArrayList As ArrayList = New ArrayList()
myArrayList.Add(1)
myArrayList.Add(2)
myArrayList.Add(3)
myArrayList.Add("Ain't ArrayList Great!")

In this example, three Integers and then a String are added to the same ArrayList. This works pretty well ... except when it comes time to do something with the ArrayList.

Dim total As Integer = 0
Dim val As Integer
For Each val In myArrayList
   total = total + val
Next

As the illustration below shows, strings and integers don't mix when you try to add them. The program crashes!

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

This problem is the heart of what is called Type Safety. The code compiles perfectly ... no problem. But it crashes when the program runs. This can be the worst kind of error because the program can be tested and put into production and then crash after everyone thinks it works. (It always happens when you're on vacation 5000 miles away!) The goal is to find potential problems at compile time when it's easy to fix them. Generics take huge strides toward this goal.

Dim myGenericList As List(Of Integer) = New List(Of Integer)
myGenericList.Add(1)
myGenericList.Add(2)
myGenericList.Add(2)
myGenericList.Add("But Generic List is Better!")

As the illustration below shows, Intellisense displays an error and the project won't compile with this potential error. Trust me, it's a lot better than having the program crash when you're on vacation. (Option Strict On must be specified for the VB.NET compiler to find this error.)

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

We round out our review of new features with Operator Overloading, IsNot, Using, and Continue on the next page.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.