Object Initializers in VB.Net
In my last post I have used a special syntax for creating instance of Person Class.
VB.Net
Dim p as New Person With {.Name = “Person1″, .Age = 20}
C#
Person p = New Person {Name = “Person1″, Age = 20};
Which is equivalent to
VB.Net
Dim oPerson as new Person oPerson.Name = "Person1" oPerson.Age = 20
C#
Person oPerson = new Person(); oPerson.Name = "Person1"; oPerson.Age = 20;
This is another new feature introduced in VB.Net 9.0, called Object Initializers. You can get more information about this in MSDN. Actually I am planning to write a post on the feature newly available in VB.Net 9.0
Your first query in LINQ
Microsoft added lot of new technology features in .Net 3.0 / 3.5. LINQ is newly added feature of .Net. From the official LINQ home page, The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. Few days back I come into a situation where I need to reduce the hit count of a particular method*, which returns a collection class, based on an argument to the method. initially I played around select case,
if – else etc, but then I come to know that the parameters can be changed. So started with LINQ.
'Creating the collection
Dim oPersonCollection As New PersonCollection()
'Adding items to the collection
oPersonCollection.Add(New Person With {.Name = "Person1", .Age = 20})
oPersonCollection.Add(New Person With {.Name = "Person2", .Age = 26})
oPersonCollection.Add(New Person With {.Name = "Person3", .Age = 23})
oPersonCollection.Add(New Person With {.Name = "Person5", .Age = 22})
oPersonCollection.Add(New Person With {.Name = "Person7", .Age = 29})
oPersonCollection.Add(New Person With {.Name = "Person4", .Age = 25})
oPersonCollection.Add(New Person With {.Name = "Person9", .Age = 20})
'Querying the collection to get all Persons from the
'collection with Age >= 20 and Age = 20 And oPerson.Age <= 23
'Binding the list to Dropdown
Me.ComboBox1.DataSource = Query.ToList()
And the implementation of PersonCollection and Person class.
Public Class PersonCollection Inherits List(Of Person) 'No Implementation Reqd. You can use a simple List or Collection. End Class 'Simple Person class, with Name and Age Properties Public Class Person Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Private m_Age As Integer Public Property Age() As Integer Get Return m_Age End Get Set(ByVal value As Integer) m_Age = value End Set End Property 'To display the value, by directly binding to the listbox. Public Overrides Function ToString() As String Return m_Name & " with an age of " & m_Age End Function End Class
Also VB.Net contains support for XML too.
Links : Official LINQ Project Page
* For last few days I am working on a WPF Project, in the Role of “Performance optimizer”