Microsoft’s Visual Basic 2008, also known as VB 9, is the third version to be based on the .Net Framework.
An added bonus is that the Express variant is free, unrestricted, and co-exists with earlier versions. With .Net established, Microsoft is evolving the language with important changes.
Perhaps the first thing to get your head round is type inference. The idea is that typing Dim i As Integer = 3 is a waste of space.
The compiler can see that 3 is an Integer, so it can infer the type. In VB 2008, if you type Dim i = 3 then the variable will be assigned the Integer type. This is at compile-time, not at runtime, so it is the same as strong typing.
Type inference only applies to local variables, and only when declared and assigned in the same statement. It could break existing code, if you were relying on VB to declare untyped variables as objects, but such errors are easy to fix.
Another space saver is called relaxed event handlers. In VB 2005, every event handler has arguments for sender and EventArgs, like this: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
In VB 2008, you can have this instead: Private Sub Button1_Click() Handles
Button1.Click
It is another step towards uncluttered code, which is a good thing.
Other new language features include Object Initializers – which means you can create an object and set its properties in the same statement – as well as Anonymous types, Lambda expressions, and Extension methods.
Although interesting, they are also part of the plumbing behind the headline feature, Linq (Language Integrated Query). Linq brings database queries into the language, so there’s no need to assemble SQL strings.
Use XML literals
New in VB 2008 is native XML. This looks strange, as it breaks assumptions about
how code is separated from data. This is now valid VB 9 code:
Dim theItems = _
<posts>
<myitem title=”First item” desc=”Some text” link=”http://someurl”/>
</posts>
The above code uses type inference to create a variable of type XDocument. It is interesting, but not yet useful. The value becomes apparent when you combine it with another feature called embedded expressions. Here’s how you can use this to transform the XML snippet above to a valid RSS feed.
All Software Applications
