Object Initializers
When LINQ was introduced in Framework 3.5/VB.NET 2008, a lot of features had to be added to make it work. One of those is called object initializers. Since it's entirely optional for other code and it makes things faster and easier, you might not be taking advantage of it.
The idea is that you can initialize a complex data object without explicitly calling a constructor. (The compiler will do it for you.)
Here's a simple example of a data object:
Public Class EmployeePublic id As IntegerPublic firstName As StringPublic lastName As StringEnd Class
This could also be a Structure instead of a Class.
Before VB.NET 2008, you had to use code like this to initialize an instance of the Employee data object:
Dim myEmp As New EmployeemyEmp.id = 1myEmp.firstName = "Dan"myEmp.lastName = "Mabbutt"
Object initializers let you use this syntax instead:
Dim myEmp As New Employee With{.id = 12345, .firstName = "Dan", .lastName = "Mabbutt"}
Object initializers also work when a method in the object takes a parameter. Suppose the New method in the Employee class requires an id parameter. For example, if the Employee class has a New subroutine like this ...
Public Class EmployeePublic id As IntegerPublic firstName As StringPublic lastName As StringPublic Sub New(ByVal value As Integer)id = valueEnd SubEnd Class
... then this works ...
Dim myEmp As New Employee(12345) With{.firstName = "Dan", .lastName = "Mabbutt"}
In fact, with VB.NET 2008, you don't actually have to create a data object. VB.NET will do it for you using another new feature called Anonymous Types. This works without any class definition at all.
Dim myEmp = New With{.id = 12345, .firstName = "Dan", .lastName = "Mabbutt"}
Anonymous types are covered in another Quick Tip: Anonymous Types - What and How
Source...