/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





You probably heard that Visual Studio 2008 lets you select the target framework version so you can use it to compile .NET 2.0 applications. I suspect you also heard about all the new cool features VB 9 includes like stuff like LINQ etc. Just in case you didn't make sure you read up on Overview of Visual Basic 9.0 by Erik Meijer, Amanda Silver, and Paul Vick.

And you probably assumed all those cool new features would only work if you where targeting the 3.5 framework, I know I did Smile. Well guess again! Some of these features are only syntactic sugar completely handled by the VB compiler.

Some of the features you can use are implicitly typed variables, both with simple and complex types.

Dim aValue = 1

'aValue = "Demo"

Console.WriteLine(aValue)

Note that assigning a string to aValue produces a compile error!

Dim home As New Country With {.Name = "the Netherlands"}

 

Another nice thing you can use is the new short notation for nullable types.

Dim aNullableInt As Integer?

aNullableInt = Nothing

 

And best of all is Lambda expressions! All they are is syntactic sugar and the compiler can take care of them. So much nicer than before Smile

Dim countries As New List(Of Country)

countries.Add(home)

countries.Add(New Country With {.Name = "France"})

countries.Add(New Country With {.Name = "Belgium"})

Dim found = countries.Find(Function(c As Country) c.Name = "France")

 

 

And just to show what is actually happening behind the scene below is the code as decompiled using Reflector.

<StandardModule()> _

Friend NotInheritable Class Module1

' Methods

<DebuggerStepThrough, CompilerGenerated> _

Private Shared Function _Lambda$__1(ByVal c As Country) As Boolean

Return (c.Name = "France")

End Function

 

<STAThread()> _

Public Shared Sub Main()

Dim aValue As Integer = 1

Console.WriteLine(aValue)

Dim VB$t_ref$S0 As New Country

VB$t_ref$S0.Name = "the Netherlands"

Dim home As Country = VB$t_ref$S0

Dim countries As New List(Of Country)

countries.Add(home)

VB$t_ref$S0 = New Country

VB$t_ref$S0.Name = "France"

countries.Add(VB$t_ref$S0)

VB$t_ref$S0 = New Country

VB$t_ref$S0.Name = "Belgium"

countries.Add(VB$t_ref$S0)

Dim found As Country = countries.Find(New Predicate(Of Country)(AddressOf Module1._Lambda$__1))

End Sub

End Class

 

Note that the compiler generated a Lambda function in this case because I used a constant string in the search filter. If I had used a variable it would have generated the required closure as an embedded type.

Enjoy VB9!

© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC