BT

Visual Basic 10: Rounding the Edges

| by Jonathan Allen Follow 636 Followers on Nov 04, 2008. Estimated reading time: 2 minutes |

Like C# 4, VB 10 isn't going to see a lot of ground breaking features. Everything so far was already available, just not in a convenient form.

The most anticipated feature is probably the near elimination of line continuation characters. While there are a few ambiguous cases, the vast majority of the time an underscore will not be necessary.

Next up is implicit array initialization. VB 9 added array initialization; unfortunately the syntax is rather verbose. Even with type inference, redundant type information is needed.

Dim vectorA As Integer() = New Integer() {1, 2, 3, 4, 5}
Dim vectorB = New Integer() {1, 2, 3, 4, 5}

Visual Basic 10 adds type inference for initialization arrays.

Dim vectorC = {1, 2, 3, 4, 5} 

This syntax works by determining the most specific type common to each argument. If that is System.Object, then a compiler warning is issued. In addition to vectors, matrix and jagged arrays are supported.

Dim matrix = {{1, 2, 3}, {4, 5, 6}}
Dim jagged = { ({1,2,3}), ({4,5}), ({6})}

Matched with this feature is collection initialization. Using the From keyword, each item in the initialization list is added to newly created object.

Dim list As New List(Of Integer) From {1,2,3,4}
Dim dictionary As New Dictionary(Of String, Integer) From {{ "Tom",80},{ "Frank",85}}

Initialization can be done with any number of arguments so long as the collection has a method called Add with the right parameters. If such a method doesn't exist, it can be added as an extension method.

Dim customers As New List(Of Customer) From {
    {"Tom", "T", "Jones"},
    {"Frank", "M", "Burns"}}

<Extension()> Sub Add( ByVal list As List(Of Customer),
                       ByVal firstName As String,
                       ByVal middleInitial As String,
                       ByVal lastName As String)
    list.Add(New Customer(firstName, middleInitial, lastName))
End Sub

Both single line and multi-line lambdas will be supported in VB 10 for functions and subroutines. The syntax for multiline delegates is show below.

listA.ForEach(Sub(id)
                  Dim c = GetCustomer(id)
                  If c.UnpaidBill > 0 Then c.Send(Invoice)
              End Sub)

Automatic properties were added using the existing syntax. As it also looks a lot like the syntax for public member variables, it should discourage use of the latter. An optional initializer is shown below.

Public Property Score As Integer = 5

C# is not the only language adding support for optional parameters. While VB had it all along, there was an supported use case. Specifically, any type defined as Nullable(Of T) wasn't allowed to be optional. This will be fixed with VB 10, finally allowing stored procedure calls to be mapped one to one with VB wrapper functions.

Up last is support for Co- and Contra-Variance. We covered this in the article C# Feature Focus: Co- and Contra-variance.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT