Automatic Properties and List Initializers


Up until VB 2010 the only way you could declare a property is by creating a private backing field for it and then setting the property up so that it stored the value in the backing variable. This makes sense if you ware needing to do some sort of calculation or if a property was dependent on another property. However, most of the time all you wanted to do was expose a property so that consuming code could access it. Because of this you often saw the same boiler plate code over and over again. Many times developers would copy the code for one property over to a new one and then update all the variables. This led to copy and paste mistakes as the developer forgot to change one of the variables. Not anymore!

Note this tutorial makes use of a sample VB.NET application. You can download it here:

This tutorial will introduce you to a couple of great VB.NET 2010 features: Automatic Properties and List initilizers. Both of these allow you to do the same thing you did before VB 2010, but with far fewer lines of code.

Automatic Properties

In the past if you wanted to declare a property Name as a string. The code would look something like this:

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

This would expose a property named Name that was of type string and that the calling code could either get or set. If the calling code set it to a new value that value would be stored in the backing variable _name. If the calling code asked to get the value of the Name property, the backing store _name variable would be returned. Wouldn’t it be nice if we could just declared the property Name and have Visual Basic automatically create the Getter and Setter code as well as the backing variable. Now we can – the code below is equivalent to this code but with far fewer lines:

Property Name As String

That’s it – that’s all we need to do. VB 2010 will take care of the rest and the calling code won’t know any difference. Either way as far as its concerned we have a property of type string with the name Name.

List Initializers

Another great feature Visual Basic .NET 2010 introduced is List initializers. This again is best explained by an example. In the past if I wanted to create a new list and populate it with some values my code would look something like this:

Dim custList As New System.Collections.Generic.List(Of CustomerExpanded)
Dim cust1 As New CustomerExpanded With {.Name = "Adventure Works", _
			.CustomerID = 13302, _
			.City = "Louisville", _
			.State = "Kentucky"}
Dim cust2 As New CustomerExpanded With {.Name = "Coho Winery", _
			.CustomerID = 38847, _
			.City = "Springfield", _
			.State = "Missouri"}

Notice I not only have to declare my custList variable and new it up, I also have to declare each customer item variable, new it up, and then add it by hand to my custList variable. Not very convenient because I really have no need to store cust1, cust2, etc. I really just want them all to be in my list and then I will use them from there.

With list and collection initializers I can do just that. I can declared a new List and then tell it want I want to populate the list with. Here is what the initializer code looks like:

Dim custList As New System.Collections.Generic.List(Of Customer)
custList = New List(Of Customer) From {
	New Customer With {.Name = "Adventure Works",
		   .CustomerID = 13302,
		   .City = "Louisville",
		   .State = "Kentucky"},
	New Customer With {.Name = "Coho Winery",
		   .CustomerID = 38847,
		   .City = "Springfield",
		   .State = "Missouri"},

This isn’t as big of time saver as automatic properties are but as you use it more and more I think you will agree that its much more helpful then having to do each Add separately. It also is less error prone because you are adding the items to the list as you initialize them. Before it was easy to accidently initialize an item but never add it to the list.

About The Sample Source Code

Included with this tutorial is some sample source code that Microsoft created to demonstrate each of the things we discussed above. Here is a quick summary of what each file is for. The Customer_AutoImplemented.vb file is the main Customer class that is used in the sample program. If you look at it you will notice that it contains 4 Automatic Properties (Name, CustomerId, City, and State). You will also notice that it has a function named GetCustomerList(). This function creates a list of test customers. If you take a look at how it does this you will see it makes use if the List Initialization ability VB.NET provides.

The other file you might want to look at is the Customer_Expanded.vb file. This file is the exact same Customer class, except it uses the “old” style of initializing properties where you have a backing store. It also uses the “old” style of initializing lists where you add each item by hand. This class is not actually used in the project but is just there for demonstration purposes.

The last file in the project is the Form1.vb file. This file is the main form, which is shown when you ran the sample application. It has a BindingSource control on it and a BindingNavigator control. It also has four text boxes (one for each Auto Property in the Customer class). In the code behind file for Form1.vb you will see the following code:

Sub Form1_Load() Handles MyBase.Load
	Me.CustomerBindingSource.DataSource = Customer.GetCustomerList().ToList()
End Sub

Although this tutorial doesn’t focus on this, the sample gives you a quick example of how easy it is to DataBind in VB.NET. This code simply sets our BindingSource’s DataSource to the our Customer list (which we initialized in our GetCustomerList function). If you run the code you will see that the Navigator control allows us to flip between all our customers. Very simple.



Since the beginning of Visual Basic .NET (and even before) we have been able to create classes with properties and we have been able to create lists of things. What VB.NET 2010 did is gave use the ability to do these things with a simpler (less error prone) syntax. If you never want to use Automatic Properties or List Initializers you don’t need to, but if you do spend the time now to understand them it will save you time and headaches down the road. Plus as every programmer knows if there is an easier (more lazy) way of doing something we want to do it that way Winking smile.

If you enjoyed this post, subscribe for updates (it's free)

I can't seem to figure out

I can't seem to figure out how to do this.
Can someone explain to me how to go about doing clamart