Top Ten Things .NET Developers Will Like About Visual Basic 2010


I was at the MVP Summit 2010 and they were interviewing and filming “What do you like About Windows 7?” I didn’t interview because at the time the only thing I thought of was that Windows 7 wasn’t getting in my way. But, using VB to write my column and blogs, it was a lot easier to figure out what I like about VB 2010.

I will share my thoughts with you, including some code snippets to demonstrate some of the features. Sound off to let me know what you love about VB 2010.

In the spirit of Senior Letterman the top ten things I like about VB 2010 are number 10:

  • 10. Generate from usage – using classes and members before you define them

  • 9. Highlighting references – click a symbol and all instances of that symbol are highlighted

  • 8. Intellisense completion and suggestion mode – see number 10

  • 7. Navigate to – search for a symbol in source code

  • 6. Collection initialization – create a collection and initialize it

  • 5. Auto-implemented properties – abbreviated properties for getters and setters

  • 4. Covariance and contravariance – use more derived parameter types

  • 3. Multiline Lambda expressions – sometimes even a Lambda needs a couple of statements

  • 2. Lambda expression support for sub – duh!

  • 1. Implicit line continuation characters – yeah!

Let’s take some time to look at these code elements and changes. The sub-sections describe some of the features mentioned. For more on line continuation characters see my article, “Goodbye to Line Continuation Characters“.

Generating (Code) From Usage

Do you do this? Write some code, access a member, and then, think, oh I have to write that class and implement that member? Now you can let Microsoft Visual Studio stub the class and members out.

Suppose that you write the code shown in Figure 1 and provide in Listing 1. The IDE will add the squiggle indicating that the type Primes is not declared. You can select from the Error Correction Options represented by the red underline and select from one of the options-see Figure 2. Click Generate ‘Class Primes’ and the IDE will add a new file and the new class or module for you. Click the Error correction Options for IsPrime and the IDE will generate the method stub for you too-see Figure 3. Listing 2 contains the generated code. All you have to do is write the statements that implement IsPrime.

Figure 1: Go ahead and use types and members as if they existed and let the IDE generate the type and member stubs for you.

Figure 3: The IDE will generate undeclared types and undeclared members.

Listing 1: Uses an undeclared type and member; let Microsoft Visual Studio 2010 generate the type and member stubs for you.
Module Module1

   Sub Main()
     Console.WriteLine(“is {0} prime: {1}”, 5, Primes.IsPrime(5))
   End Sub

End Module

Listing 2: The code generated based on the usage of Primes and IsPrime.

Class Primes

 Shared Function IsPrime(ByVal p1 As Integer) As Object
   Throw New NotImplementedException
 End Function

End Class

For an implementation of IsPrime see my January 5th, 2009 article on Dr. Dobbs located here, or the VB6 version based on the Sieve of Eratosthenes on CodeGuru.

Navigate to

The Navigate To feature is an item on the Edit menu in Microsoft Visual Studio. To see all of the results click Edit|Navigate To. Type the term in the Search Terms edit field and click on a result in the results pane (see Figure 4). This is a simpler form of using the Object Browser.

Figure 4: Enter the search term and click on one of the items in the results list to change the focus of Visual Studio to that item.

In the example I navigated to the implementation of IsPrime and began stubbing out the implementation shown in Listing 3.

Listing 3: The Sieve of Eratosthenes used to calculate primes from 2 to 1,000,000.

Class Primes

 Private Shared Primes As List(Of Integer) = New List(Of Integer)

 Shared Sub New()
   Primes.AddRange({2, 3, 5, 7, 11})
 End Sub

 Shared Function IsPrime(ByVal test As Integer) As Boolean

   For I = 0 To Primes.Count – 1
     If (test Mod Primes(I) = 0) Then Return False
     If (Primes(I) >= Math.Sqrt(test)) Then Exit For

   Return True

 End Function

 Private Shared Sub BuildPrimes()
   For i = 13 To 1000000
   If (IsPrime(i)) Then
   End If
 End Sub

End Class

Collection Initializers

A collection is a general term for a literal collection, array, or something that is enumerable. You and I used to have to initialize something like a List(Of T) where T is some type and then manually add elements to a collection. Collection initialization means that you can add items to a collection by using collection initialization syntax-{item1, item2, item3,…itemn}. The shared constructor in Listing 4 demonstrates a collection initialize used in the AddRange method.

Primes.AddRange({2, 3, 5, 7, 11})

You could also write the code something like this:

Dim somePrimes = {2, 3,5,7,11}

You can use collection initialization syntax to initialize a collection of composite objects too.

Auto-Implemented Properties

Auto-implemented properties are property statements with explicit getter and setter blocks. The backing field is generated by the compiler. When you use auto-properties it means you will be referring to the property and not the field. Use an auto-implemented property when the getter simply returns the field value and the setter simply assigns a value to the backing field. An auto-implemented property for the Primes (List(Of Integer)) property would be written as follows:

Public Shared Property Primes As New List(Of Integer)

Auto-implemented properties save you time writing code. (You can also use a code generation tool like CodeRush to generate properties based on a template.) If you are in the camp that thinks everything should be very explicit use a meta programming tool. And, while CodeRush and these types of tools will save you a lot of time typing using auto-implemented properties is a great time saver. It is about time VB had them too.

Undertsanding Covariance and Contravariance

I love Microsoft. I love the story, the history, the products, but I swear they have so many eggheads in Redmond that they always find the exact and most complicated way of naming things (I had to look up contravariance and covariance because they weren’t terms that I would generally use, but they’d be great Scrabble plays in 9 letter Scrabble).

Covariance means you can use a more derived type for a parameter, a child type, or something that inherits from the defined type. Contravariance means you can use a less derived type than the one specified. Since everything is derived from Object in .NET then you could replace a parameter with Object to use contravariance and a descendant type for covariance.

The whole purpose behind this is to let you more broadly match method signatures. The most common usage of this I have seen is in event (or delegate types) where there are a lot of parameters defined as Object. The variance topics described in the help files include generic interfaces, usage with collections, usage with delegates and usage with the generic delegates Func and Action. Refer to the MSDN help topic for examples on variance.

Defining a Multi-Line Lambda Sub

Originally in .NET for VB every Lambda expression had to be a Function. This was clearly some oversight weirdness on the VB team’s part. A lambda expression is simple very, very shorthand for a method. The problem in VB is that Lambdas had to return something. Sometimes you just want a method that does something without returning a value, hence the existence of Functions and Subs. With .NET 4.0 for VB you can define Function and Sub Lambda expressions. The first example demonstrates a Function Lambda and the second demonstrates a Sub Lambda.

Module Module1

   Sub Main()

     ‘ a function Lambda
     Dim PrimeTest = Function(number) Primes.IsPrime(number)

     ‘a sub Lambda
     Dim WriteLine = Sub(mask, number) Console.WriteLine(mask,
       number, PrimeTest(number))

     WriteLine(“is {0} prime: {1}”, 5)

   End Sub

End Module


I wrote my first lines of code in 1978. Coding was slow, laborious and tedious, but we wrote a lot less of it in those days. Now we write applications that have hundreds of thousands of lines of code. New features like auto-implemented properties and Lambda expressions have been added to VB to make programmers more productive. Combine these new features with meta-programming tools and you can fly through code.

Now there are some people that like to hold onto the old ways. Be explicit, verbose, but that is a comfort factor usually found with guys my age. I like new inventions in programming and embrace them. For me it all comes down to productivity.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read