VB9 Features

I’ve just noticed, care of Bill and Beth, that the VB9 features article has been updated as of February this year.  After a quick scan there are some clear changes that look great (and some that don’t)!

Object Initializers

Dim c as New Country With { .Name = “Palau”, .Area = 458, .Population = 16952 }

What I like about this notation is that it follows the VB tradition of using the With statement.  Previously you would have written:

Dim c as New Country()
With c
End With

The new notation is particular efficient if you want to initialise a collection of Country objects as each object can be instantiated and properties set in a single statement. For example:

Dim countries = { _
   New Country With { .Name = “Palau”, .Area = 458, .Population = 16952 }, _
   New Country With { .Name = “Monaco”, .Area = 1.9, .Population = 31719 }, _
   New Country With { .Name = “Belize”, .Area = 22960, .Population = 219296 }, _
   New Country With { .Name = “Madagascar”, .Area = 587040, .Population = 13670507} _

Implicity typing

In the last section you will have noticed that I didn’t specify the type of the countries collection.  This is NOT because I haven’t allowed VB.NET to run riot, it is because we have implicit typing.  This defines countries to be of type Country(). Why, because the right hand side defines an array of Country objects – we can see that, and now the compiler can too.

WARNING: To all those developers (including C# developers using the new var keyword) out there who are going to start using this feature throughout their code you are as bad as those VB6- developers who used Variant objects, or for that matter .NET developers who use object objects cause they can’t be bothered getting it right.  The reason for this is that it makes your code harder to read and understand – you can’t just read the variable declaration and know what type it is, instead you have to dissect the right hand side.  The ONLY time IMHO that implicit typing should (and in fact sometimes has to) be used is in a Linq statement.


Yeh, yeh, yeh – by now I’m hoping we have all heard, if not seen, Linq.  So just for clarrification we are talking about:

Dim smallCountries = From country In countries _
                                  Where country.Population < 1000000 _ 
                                  Select country

Of course it goes without saying that this is fundamentally cool! Before we go further though, it is worth noting that smallCountries is an IEnumerable(of Country) – if you aren’t comfortable with Interfaces, now would be great time to begin using them!

Lambda Expressions


Function(Country As Country) country.Name
Function (Country As Country) country.Population < 1000000

These can be written as inline (like mini-anonymous methods) such as:

Dim smallCountries = _
   Enumerable.Select( _
      Enumerable.Where(countries, _
         Function (country As Country) country.Population < 1000000), _ 
      Function(country As Country) country.Name

IMHO this is probably one of the weakest of the new features available in VB9.  The VB.NET community has long been asking for both anonymous methods and interators.  Instead of giving us C#2.0 feature parity the team has leap frogged into Linq giving us only minimal lambda expressions.  Luckily this will only affect the hardcore developers out there who are building collections and doing fancy framework development (damn, looks like I might have to go back to C# afterall).

Nullable Types

Ok, so we have seen this before but this time we have the ? syntax, just like C#v2:

Partial Class Country
   Public Property Independence As Date?
End Class


There are a bunch of other features such as XML support and Relaxed delegates that didn’t make this blog post.  I’d encourage you to read the full article and be prepared.  These features, especially IMHO Linq, are going to have more impact than Generics did!

Leave a comment