What’s new in Visual Basic .NET (Part 1)

In previous posts:

What’s new in C Sharp (Part 2)
What’s new in C Sharp (Part 1)

I wrote about some new features in C Sharp 3, integrated in Visual Studio 2008 (I’m using the Bete 2).

Now, I have an example, in VB.NET 9 (the new version of the language), to download from VbNetNew1.zip

It’s a simple console application, that has a class Supplier:

Public Class Supplier 
    Private mSupplierId As Integer 
    Private mName As String 
    Private mCity As String   

    Public Sub New(ByVal id As Integer) 
        mSupplierId = id 
    End Sub   

    Public Property SupplierId() As Integer 
            Return mSupplierId 
        End Get 
        Set(ByVal value As Integer) 
            mSupplierId = value 
        End Set 
    End Property   

    Public Property Name() As String 
            Return mName 
        End Get 
        Set(ByVal value As String) 
            mName = value 
        End Set 
    End Property   

    Public Property City() As String 
            Return mCity 
        End Get 
        Set(ByVal value As String) 
            mCity = value 
        End Set 
    End Property 
End Class 

I didn’t found any support in the new VB.NET for automatic properties, as in C Sharp. So, in this example, the class code has no new feature.

At subroutine Main:

 1     Sub Main() 
 2         Dim suppliers() As Supplier = New Supplier() { _ 
 3             New Supplier(1) With {.Name = "John Madison",   
.City = "Washington"}, _ 
 4             New Supplier(2) With {.Name = "Ann Arbor", 
.City = "Ohio"}, _ 
 5             New Supplier(3) With {.Name = "Don Johnson", 
.City = "Miami"} _ 
 6         } 
 8         For Each supplier As Supplier In suppliers 
 9             Console.WriteLine("{0} {1}", 
supplier.SupplierId, supplier.Name) 
10         Next 
12         Dim c = New With {.Name = "John Doe",   

.City = "Everywhere"} 
14         Console.WriteLine("{0} lives in {1}", c.Name, c.City) 
16         For Each supplier As Supplier In suppliers 
17             supplier.Print() 
18         Next 
20         Console.ReadLine() 
21     End Sub 

there are some new features in use. At line 2, you see the creation and initialization of an array, using brackets. At lines 3, 4, and five, the creation of objects uses With. Using this word, we can call a constructar, as Supplier(1), and then complete the properties, using point and the property name.

At line 12, a variable is created with an implicit type. The new doesn’t refer to any class, but using with the properties can be defined.

Then, IDE “makes”, infers the type of variable c, and we can use  c.Name and c.City as at line 14.

At line 17, Print is not a method of class Supplier. It’s defined in other “class”:

Imports System.Runtime.CompilerServices   

Module Extensions   

    <Extension()> _ 
    Public Sub Print(ByVal supplier As Supplier) 
        Console.WriteLine("Supplier {0} {1}", _ 
supplier.SupplierId, supplier.Name) 
    End Sub   

End Module 

This is an extension method: a method that can be defined to all instances of a class, out of this class definition. In CSharp, we use static classes to this purpose. Here, in VB.NET, we use modules. To be an extension method, in CSharp a this is used in the first argument. Here, an attribute <Extension()> is attached to the method definition. That attribute is defined inside namespace System.Runtime.CompilerServices.

Angel “Java” Lopez

1 thought on “What’s new in Visual Basic .NET (Part 1)

  1. Pingback: What’s new in Visual Basic.NET (Part 2) « Angel “Java” Lopez on Blog

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s