Visual Basic 9 for the Insane – The rise and fall of "As" – Implicit Types



It was awfully hot in the training room today.  I’m not sure why, as the corridor seemed quite cool.   Anthony our trainer was convinced it was the air conditioning that was the problem however.   He agreed with Jack who said the presence in our room of a pedestal fan bode badly for hopes of good air supply.

I think it the problem may have been the emotional turmoil caused to the VB developers in the room when shown how “implicit types”  (aka Type inference)  makes this kind of syntax ok:

Dim MyName = "Trouble"

Now in the days of VB3-6, VBScript and VBA the non-explicit creation of a variable without a type  (a variant) was a “feature” of the language.  “Serious” Visual Basic developers did not use this feature.  They differentiated themselves using the “Option Explicit On” feature to ensure that it couldn’t be used.

(Of course all this was nothing compared to the nausea experienced by our CSharp cousins with the var keyword….I just love telling them “var” is short for “variant”)

In VB.Net, this was the default, so we got used to:

Dim MyName As String = "Trouble"

VB9 (and CSharp 3.0) introduces a “compiler trick” called “type inference” to dimension a variable to the appropriate type as part of the compilation process.

This means, that unlike the original variant type, you get compile time checking and intellisense.  This is cool.

This also works for iterators in loops:

Dim numbers = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9}
For Each num In numbers
    If num > 2 Then Console.WriteLine(num.ToString)

In this case, liNumbers is implicitly an integer array and num is a single integer.  This works for objects too.  (Do you also recall how proud we were when As was supported in For.. operators in 1.1? …. oh well)

This capability was introduced into the language to allow LINQ expressions to allow results to be assigned without having to create a type beforehand.  In order to stay true to my mandate of simplicity, I’ll leave that example to another time.  


Visual Basic 9 for the Insane – An Introduction


One response »

  1. Pingback: Visual Basic 9 for the Insane - An Introduction « An Original Idea

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s