5,696,038 members and growing! (16,445 online)
Email Password   helpLost your password?
Languages » VB.NET » Visual Basic .NET License: The Code Project Open License (CPOL)

Object Initialization Expressions and Anonymous Types in Visual Basic 9.0

By Alessandro Del Sole

An introduction to some new cool features of the Visual Basic 9.0 language syntax
VB (VB 7.x, VB 8.0, VB 9.0, VB 6, VB)

Posted: 9 Mar 2008
Updated: 9 Mar 2008
Views: 3,229
Bookmarked: 2 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
1 vote for this Article.
Popularity: 0.00 Rating: 2.00 out of 5
0 votes, 0.0%
1
1 vote, 100.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Visual Basic 9.0 introduces some new language features like Object Initializers and Anonymous Types. These features are really important to understand if you plan to learn LINQ, the major new feature in .NET Framework 3.5.

In this article I will introduce Object Initializers and Anonymous Types in Visual Basic 2008, providing some code snippets to make concepts clearer.

You are free to use Visual Basic 2008 Express Edition or greater, since the compiler is always the same.

Object Initialization Expressions

Object Initialization Expression is about instantiating objects whose constructor receives no arguments even if objects themselves exposes one or more property which can be initialized. To accomplish this, in Visual Basic 9.0 we can use the With keyword. Consider the following, very simple, Person class which exposes two properties Name and Surname:

Class Person

    Private _name As String
    Private _surName As String

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

    Public Property SurName() As String
        Get
            Return _surName
        End Get
        Set(ByVal value As String)
            _surName = value
        End Set
    End Property
End Class

As you can see no explicit constructor has been specified. This means that the class implements a constructor that does not receive any arguments. To instantiate this class using Visual Basic 8.0, we had to write the following code:

Dim p As New Person

With p
     .Name = "Alessandro"
     .SurName = "Del Sole"
End With

Now, in Visual Basic 9.0 we can use the following syntax:

'Object Initialization Expression
Dim p As New Person With {.Name = "Alessandro", .SurName = "Del Sole"}

This is very useful both for writing less lines of code and for keeping code clearer. Many WPF objects have constructor that receives no arguments, so this is another good chance to instantiate objects in the base class library.

Anonymous Types

Another new interesting feature of .NET Framework 3.5 is in the so called Anonymous Types. Substantially when you instantiate an object you don’t need to specify its type but you can populate it with elements of your own interest. This must not be confused with the Local Type Inference, another .NET 3.5 feature, where is the compiler which automatically detects the type which an object belongs to. Anonymous types are really important in LINQ query expressions. Now, let’s see a pair of code snippets. Consider the following snippet:

'Anonymous type
Dim firstAnonymous = New With {.Name = "Alessandro", .Age = 30}

This instantiates a firstAnonymous object which is an anonymous type. As you can see, after the New keyword no type is specified but the object is initialized and populated with two members, which are respectively a String and an Integer (here we have used again the Object Initialization Expression technique). These members are considered like normal properties and every anonymous type must contain at least one. Moreover, anonymous types can contain read-only properties. To accomplish this you must mark each property with the Key keyword, as you can see in the following code snippet:

'Anonymous type with two readonly properties
Dim secondAnonymous = New With {Key .Name = "Alessandro", Key .Age = 30}

It’s really important to consider that anonymous types are only visible from within the members that define them. This should be not a big problem since, as we said before, anonymous types’ main usage is within LINQ query expressions and not at class or module level.

Points of Interest

Those we just saw are two really important language features in VB 9.0. Should take a deeper look if you plan to seriously use LINQ for data queries.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alessandro Del Sole


I'm an Italian .NET developer and I write articles and books about Visual Basic 2005/2008, WPF and VSTO.

Check out my Italian blog at: http://community.visual-basic.it/Alessandro
Occupation: Other
Location: Italy Italy

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Mar 2008
Editor:
Copyright 2008 by Alessandro Del Sole
Everything else Copyright © CodeProject, 1999-2008
FileMail | Advertise on the Code Project