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)


Good opinion indeed! Everybody knows that students want to combine work and studies. In that case if student earn money, his college assignments, like custom writing couldn't be successful. Will look for more identical information.

Mobile wallets

I really appreciate the fact that you approach these topics from a standpoint of knowledge and information. Mobile wallets

The Best Online Games using programming

Basic programming will be usefull for our students in gaming especially for improve their logic.


Be a yardstick of quality. Some people aren't used to an environment where excellence is expected.
flower magic


For your bride’s mother wedding speech’s practice, don’t forget about to think about a few things. You need to exercise next to your loved ones or buddies so you can get good feedback and advices if it still needs enhancement.Yacon

Thanks for taking the time to

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

red bottom shoes

Obviously the internet, Google, Yahoo, Youtube and other search engines have had an incredible influence Once you stack up on all such free coupons it will become a lot easier for you louis vuitton to get the best deals on what is needed Also, use good pictures that relate to your post" Immediately after some further investigation, I personally realized michael kors outlet there was a number of people finding involved, and the idea was free to start, so I stated just what the actual heck? What's the worst louis vuitton outlet that could occur? I personally decided to offer it a try plus within 2 days I personally had my 1st commission Here's how you can do Christian Louboutin outlet online to achieve the minor repairs yourself without disastrous results If you do not grab the sort of customers who have a need to obtain what you Authentic Louis Vuitton Handbags are providing, then unquestionably they will not buy the itemThe second part teaches you who to create high quality products easily, which range from Private Label michael kors outlet Rights strategies to outsourcing Working with an experienced plumber, you learn the skills to manage a business, dealing with situations that can not be treated in louis vuitton outlet textbooks, and realistic estimates of how it makes its way through life as a plumber The reason for this is simple: the manufacturing processes for architectural michael kors bags terracotta did not allow large pieces to be produced Whatever you're specific needs plumbers Exeter; you want to be sure that you have chosen the most louis vuitton handbags reputable professionals Grab it Now While Supply Lasts In this pair, the base louis vuitton outlet online currency is the Euro and the "basis" for buying or selling of DollarsThere is no such thing as a perfectly foolproof storage safety arrangement If everyone became rich then they would have an increase in cash flow because they control the Fortune 500 companies and the New louis vuitton outlet York Stock Exchange How hard would it be to simply switch the light off when you get out of a room? In the daytime, the outside gucci outlet light is typically sufficient, so that the lights don't need to be flipped on inside, unless they are truly needed The photograph ar tart, this family Louis Vuitton Sale uptodate and your layout is well navigated with the fiveyearold Fashions are fleeting, and to be of interest to todayrsquo;s fastpaced consumers, businesses have to keep christian louboutin shoes outlet reinventing themselves What you do not want to do is loose the listener at the beginning of the call It only makes since to utilize the gucci MLM PLR Store as your mlm content source Not just will you boost the traffic in your website, it will likewise tell your buyers that you louis vuitton purses know their demands and that you care what they want Your car or truck coverage will take care of a number of the automobile accident expenseTotal michael kors purses Traffic Annihilation consist of one wordpress plugin, 11 training videos, one main pdf of 114 pages and other full content pdf's The survey results could be louis vuitton bags of interest to any organisation that has a supporter\donoe base and is concerned with churn, attrition and retention rates Cheaper AC initially will cost less up michael kors front but will not last as long To SMSDailys credit, they don't claim to be a network marketing business and make it obvious on the firm louis vuitton outlet store site that they're just an affiliate marketing program

A lot of people in our

A lot of people in our industry haven't had very diverse experiences. So they don't have enough dots to connect, and they end up with very linear solutions without a broad perspective on the problem. The broader one's understanding of the human experience, the better design we will have.
cna classes


Incredible!! Well done !! That is really appreciable work. We must encourage these talented people for using their skills and abilities for the welfare of the children suffering from autism.PassbeeMedia

URL for HealthbodyINC

i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me.URL for HealthbodyINC

Best Manpower Recruitment

Best Manpower Recruitment agencies in Pakistan, Just as only a few seeds manage to measure and grow from lack of care.


There is no question that instantaneous water heaters are good options to trust in terms of their water heating services. However, you need to know that not all kinds of this water heater will work best for you.


Zest is the secret of all beauty. There is no beauty that is attractive without zest.
Timber Merchants Liverpool


I am impressed by the quality article you have shared on this site and I have learned how effective social blogging can bring awareness and changes in the society.

Great information shared with

Great information shared with the users here, its noteworthy one to get a great information for future business purpose.
[ what stores sell garcinia cambogia ][ Garcinia Cambogia Walgreens ]

No content is comparable and

No content is comparable and admirable then this mentioned for the explanations, a must one to read for everyone and get assisted a lot
[ where to buy garcinia cambogia ][ where can I buy garcinia cambogia ][ stores that sell garcinia cambogia ]

Mobile wallet

which shows that this is still a special audience. But im quite sure is it gonna be very popular in the years to come.Mobile wallet

Excellent topic to have a

Excellent topic to have a short review, i am certainly impressed while having a look on to it and everyone needs to follow it now on
{ dr oz green coffee bean extract }{ verified garcinia cambogia }

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

I think your article is

I think your article is really great i love to read your post again and again and i enjoy.


There is no question that instantaneous water heaters are good options to trust in terms of their water heating services. However, you need to know that not all kinds of this water heater will work best for you.
SEO Leeds

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.