|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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
IntroductionVisual 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 ExpressionsObject 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 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 TypesAnother 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 '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 InterestThose 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.
|
||||||||||||||||||||||