Favorite Gems of Visual Basic 2008
Whew! After many years working on it, we’ve finally shipped Visual Studio 2008! It’s always so exhilarating to get the product out the door as it means that our Beta testers have said they’re satisfied with the functionality, the stability, and the performance of the product and it’s ready for primetime. It’s not until the product is used by many users for real application building that we see letters and blog posts from users reporting that they’ve noticed and appreciated the little things. When users notice those little features, we get a sense of satisfaction in Redmond because we know that we’re working on a product that makes many, many, people more productive every day.
One of our MVPs, Maurice de Beijer, asked me to write this article covering some of the little features that you wouldn’t notice if you read all the banner articles on Visual Studio 2008. These are the hidden gems in Visual Basic and Visual Studio 2008 that you haven’t read about in blog posts or seen at conferences.
Multi-Targetting
We’ve got to start with Multi-Targetting. I label this feature zero because it’s really a prerequisite for the rest of the features in this list. With Multi-Targetting, you can use Visual Studio 2008 (and VB 9!) to target the .NET 2.0 frameworks. All of the following features I’ll show you can be leveraged even if you’re targeting .NET 2.0! (With the exception of item #8 as it depends upon the LINQ to XML API which resides in .NET 3.5) To do this, when you start up Visual Studio and create a new project, just make sure that the target framework is set to .NET Framework 2.0.

Fig. 1: Multitargetting options in the New Project dialog
Note that projects you’re upgrading will automatically target .NET Framework 2.0 until you add references to .NET 3.0 or 3.5 assemblies.
Intellisense Everywhere
The first thing you’re going to jazz you is Intellisense Everywhere. In short, this feature is about providing statement completion suggestions at new points in your editing experience. Some examples are best shown as screen shots:

Fig. 2: Intellisense at the expression level.

Fig. 3: Grammatical Intellisense helps you learn the query syntax

Fig. 4: Intellisense helps you find local variables faster
You’ll quickly change the way you code with 2008. Now you’ll only have to hit 2-3 characters and hit tab to complete. The code flows out of your fingers.
Relaxed Delegates
Another great feature that you’ll notice in Beta1 is Relaxed Delegates. In short, relaxed delegates are a way to extend VB’s implicit conversions to delegate types. With relaxed delegates, you can write the following code:
Private Sub Button1_Click( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click, Button1.MouseClick
MsgBox("Do Something")
End Sub
Listing 1: Unified handler for Button click and Mouse click event
You can even omit *all* of the event arguments if your method body doesn’t need them. This improves readability without compromising type safety:
Option Strict On
Public Class Form1
Private Sub Button1_Click()
Handles Button1.Click, Button1.MouseClick
MsgBox("Do Something")
End Sub
End Class
Listing 2: Omitting the event arguments is a type safe operation and improves the readability of events and their handlers
Type Inference
In Visual Basic 9, in the following bit of code and NOTHING is late-bound – everything is bound at compile-time which means you get Intellisense and type checking.
Dim dialog = New OpenFileDialog()
Dim result = dialog.ShowDialog()
Dim printStr = "C:\"
If result = Windows.Forms.DialogResult.OK Then
printStr = dialog.FileName
End If
MsgBox(printStr)
Listing 3: Type inference allows you to write type safe code without having to explicitly name the types
This makes prototyping very quick, easy, and type-safe! This feature and the mechanisms that control it are comprehensively covered in an article by a member of our QA team, Bill Horst (see the references section).
If Operator
Ever noticed that the IIF function returns something of type Object? This means that you don’t get Intellisense or type-checking by default on that result. For those of you that insist on type-safe, early-bound code, you have to cast the result. The code then looks something like this:
Dim intC As Integer = _
CInt(IIf(intA = intB, intA, intB - 1))
With the If operator, you can now write that line as:
Dim intD As Integer = If(intA = intB, intA, intB)
And with type inference it gets even easier on the eyes:

I'm a fan of any feature that improves readability.
Object Initializers
Generally, in the .NET frameworks, we try to design APIs that use the create-set-call pattern as our usability studies have indicated they are the easiest to use by the average programmer. Nevertheless, objects that don’t have a parameterless constructor exist in the .NET framework. When that happens, it’s really nice to be able to leverage Object Initializers which are kind of like a Dim and a With statement combined into an expression. That makes parameterized constructors somewhat easier to tolerate:
Dim strm As New StreamWriter( _
New FileStream("C:\out.txt", FileMode.OpenOrCreate) _
With {.Position = 10})
It also makes creating arrays of objects much easier.
Dim Capitals() As City = {
New City With {.Name = "Antanarivo",
.Country= "Madagascar"}, _
New City With {.Name = "Belmopan",
.Country = "Belize"}, _
New City With {.Name = "Monaco",
.Country = "Monaco"}, _
New City With {.Country = "Palau",
.Name = "Koror"}}
Nullable
Nullable is the feature you’ll notice but rarely have to think about. It’s basically the .NET representation for a Nullable value type (Integer, Date, etc.) Using the designer for LINQ to SQL, the Object-Relational Mapping layer introduced in Visual Studio 2008, nullable columns in your database are mapped to this type. The result is that you can write the following expression in VB and the right thing happens – null valued rows propagate null. In the example below, the Independence property on the Country type is a nullable date, denoted as Date?
Dim virginIslands As
New Country With {.Independence = Nothing}
Dim palau As
New Country With {.Independence = #10/1/1994#}
Dim vILength = #8/24/2005# - virginIslands.Independence
' ==> Nothing
Dim pLength = #8/24/2005# - palau.Independence
' ==> 3980.00:00:00
Syntax Tooltips
How cool is this?

Or how about this?

And what do you think of this?

XML Namespace support in Intellisense
We’ve talked about XML Intellisense before. But there’s a tidbit we’ve overlooked.
When namespaces are used in the XML document you’re reading from, the Intellisense engine matches the namespace prefix and the local name. This little productivity boost can yield a significant savings in keystrokes. Instead of typing the prefix then the colon and then the local name, you just type a little bit of the local name and hit enter. Here is a small example of how it works, starting with an input document and the applicable Intellisense:

If we just type the letter “t” the “tomato” entry will be selected:

If we type the letter “e” the “exa:eggplant” will be selected and the completion list will also contain the prefix “exa” and the “elephant” entry:

The next character that will be typed will determine which single entry will get selected. Now, how easy was that?
GoTo Type Definition
Often, when you define a variable, you want to view the type definition in code or in the Object Browser. There’s a new option in the context menu which allows you to go directly to the type definition instead of just the variable declaration. This is great when combined with type inference as it allows you confirm that the type of the variable is actually what you believe it to be.

Type inference for loop variables
Check out the following code:

And this code:

Without having to specify the type of the control variable, it’s inferred from right-hand-side expression or the collection we’re iterating over.
Performance improvements and… Non-blocking operations!
The background compiler is an awesome feature that gives you immediate feedback on the correctness of the code you write. We’ve made some tremendous strides in the performance of the background compiler in this release. We believe that the background compiler is 3 times faster uses 3 times less memory. Anyone who has migrated their project to 2008 should notice an improvement after a couple minutes of use.
While we’ve made huge improvements on the throughput, certain operations like changing the declaration of a base class that’s used many times in a large project is computationally expensive. If you attempted to invoke a feature that relied on compiled information before the background compiler has a chance to do its magic, like Intellisense or the Drop Downs, previous versions of Visual Studio would grind to a halt until the compilation was complete. Those operations are no-longer blocking. So, while you might occasionally get the drop downs to look like this:

The IDE will still be responsive.
Now do you have to download it? Remember, you can always grab the Express Edition!
Resources: