Object initializers provide a simple way to call the constructor of a type and then set the values of some or all properties in a single statement. The compiler invokes the appropriate constructor for the statement: the default constructor if no arguments are presented, or a parameterized constructor if one or more arguments are sent. After that, the specified properties are initialized in the order in which they are presented in the initializer list.
Each initialization in the initializer list consists of the assignment of an initial value to a member of the class. The names and data types of the members are determined when the class is defined. In the following examples, the Customer class must exist, and must have members named Name and City that can accept string values.
Dim cust0 As Customer = New Customer With {.Name = "Toni Poe", _
.City = "Louisville"}
Alternatively, you can obtain the same result by using the following code:
Dim cust1 As New Customer With {.Name = "Toni Poe", _
.City = "Louisville"}
Each of these declarations is equivalent to the following example, which creates a Customer object by using the default constructor, and then specifies initial values for the Name and City properties by using a With statement.
Dim cust2 As New Customer()
With cust2
.Name = "Toni Poe"
.City = "Louisville"
End With
If the Customer class contains a parameterized constructor that enables you to send in a value for Name, for example, you can also declare and initialize a Customer object in the following ways:
Dim cust3 As Customer = New Customer("Toni Poe") _
With {.City = "Louisville"}
' --or--
Dim cust4 As New Customer("Toni Poe") _
With {.City = "Louisville"}
You do not have to initialize all properties, as the following code shows.
Dim cust5 As Customer = New Customer With {.Name = "Toni Poe"}
However, the initialization list cannot be empty. Uninitialized properties retain their default values.
Type Inference with Named Types
You can shorten the code for the declaration of cust1 by combining object initializers and local type inference. This enables you to omit the As clause in the variable declaration. The data type of the variable is inferred from the type of the object that is created by the assignment. In the following example, the type of cust6 is Customer.
Dim cust6 = New Customer With {.Name = "Toni Poe", _
.City = "Louisville"}
Remarks About Named Types
A class member cannot be initialized more than one time in the object initializer list. The declaration of cust7 causes an error.
'' This code does not compile because Name is initialized twice.
' Dim cust7 = New Customer With {.Name = "Toni Poe", _
' .City = "Louisville" _
' .Name = "Blue Yonder Airlines"}
A member can be used to initialize itself or another field. If a member is accessed before it has been initialized, as in the following declaration for cust8, the default value will be used. Remember that when a declaration that uses an object initializer is processed, the first thing that happens is that the appropriate constructor is invoked. After that, the individual fields in the initializer list are initialized. In the following examples, the default value for Name is assigned for cust8, and an initialized value is assigned in cust9.
Dim cust8 = New Customer With {.Name = .Name & ", President"}
Dim cust9 = New Customer With {.Name = "Toni Poe", _
.Title = .Name & ", President"}
The following example uses the parameterized constructor from cust3 and cust4 to declare and initialize cust10 and cust11.
Dim cust10 = New Customer("Toni Poe") With {.Name = .Name & ", President"}
' --or--
Dim cust11 As New Customer("Toni Poe") With {.Name = .Name & ", President"}
Object initializers can be nested. In the following example, AddressClass is a class that has two properties, City and State, and the Customer class has an Address property that is an instance of AddressClass.
Dim cust12 = New Customer With {.Name = "Toni Poe", _
.Address = New AddressClass _
With {.City = "Louisville", _
.State = "Kentucky"}}
Console.WriteLine(cust12.Address.State)
The initialization list cannot be empty.
The instance being initialized cannot be of type Object.
Class members being initialized cannot be shared members, read-only members, constants, or method calls.
Class members being initialized cannot be indexed or qualified. The following examples raise compiler errors:
'' Not valid.
' Dim c1 = New Customer With {.OrderNumbers(0) = 148662}
' Dim c2 = New Customer with {.Address.City = "Springfield"}