Find out what the new year holds for Visual Basic programmers
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 = _
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.
Related articles
Q.How do I stop Windows 7 search?
Q.Is it a genuine call from Microsoft?
Q.How can I turn Autoplay back on?
Following Paul Chambers' conviction for posting a "menacing message" on Twitter, his QC will argue at today's appeal that it was not a criminal offence
|
|
|
|
|
Nikon Coolpix S570 BlackPrice: £66.99 |
Back Issue CD-Rom 13 (2010)Price: £9.99 |
Marine AquariumPrice: £15.41 |
Print Saver EcoPrice: £19.99 |
Norton Internet Security 2012 - 3 PCs, 1 year protectionPrice: £24.99 |