Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.Net Framework 4.0: VB.NET supports automatic properties

Next version of .Net Framework brings some new features also to VB.NET. One of those new features is support for automatic properties. C# automatic properties are here from .Net Framework 3.5 and now are them also in VB.NET. In this posting I will show to VB.NET users how to use automatic properties and explain why to use them.

Take a look at the following class created in Visual Studio 2010.


Public Class Person

    Public Property FirstName As String = "John"

    Public Property LastName As String

 

    Private strSSN As String

 

    Public Property SSN()

        Get

            SSN() = strSSN

        End Get

        Set(ByVal value)

            strSSN = value

        End Set

    End Property

 

End Class


There are three properties: FirstName, LastName and SSN. FirstName and LastName are automatic properties. They are automatic because after compiling they look almost like SSN property – the variable that holds property value is created automatically. If you are interested in internals of automatic properties then you may read my blog posting Is Automatic Property Same as Property?

Property initialization

As you can see from class above you can assign initial values to automatic properties. You don’t have to write constructor to class just to initialize properties that have simple value types. Of course, compilator generates default constructor if you don’t write one but what you win is shorter code.

Less unit test

SSN is classic property and it is up to programmer to handle value of the property correctly. As people are error prone then it is easy to make mistakes in classic properties. Specially if internal variables have similiar names. To avoid those problems you write unit tests for properties – just to make sure your properties are handling their internal variables correctly.

With automatic properties you don’t need those tests. Testing automatic property is pointless – if it is broken then all the .Net Framework is broken and I am pretty sure you are not able to run Visual Studio in this case.

If you have 100 classes and each of them has 10 simple properties you can remove 1000 unit tests if you start using automatic properties.

Less code

The main point of automatic properties is to avoid writing unnecessary code. When I converted one of my code examples from C# to VB.NET I was amazed how long code I got. Of course… I decided not to publish this code here (imho every good programmer should know some Java-like language and I really ignore whining like why-this-code-is-not-vbnet).

If you look at code example above and make some easy calculations then you should find out that you win 7 lines on code per one property if you start using automatic properties. For class with 10 simple properties where property only returns or sets the value of internal variable you can delete 70 lines of code from your class. If you have 100 classes with 10 simple properties you can delete 7000 lines of unnecessary code. Cool, isn’t it?

Conclusion

Automatic properties are really powerful feature that helps you keep code shorter and easier to read and maintain. Also you don’t have to worry about typos in internal variable names that properties use. As a blogger I am happy because now I can publish VB.NET examples besides C# ones and I can be sure that these examples are not horribly long due to unnecessary code anymore.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!

Comments

Niclas Pehrsson said:

How can I make the setter private and the getter public in this scenario? like in c#

public string Name { get; private set; }

# November 1, 2009 8:53 AM

DigiMortal said:

Not sure if it is already implemented. There is one forum thread that you may find useful: social.msdn.microsoft.com/.../9d62463e-aec7-451d-9f10-89477416d26a I hope that in first stable version automatic properties work exactly like they work in C#

# November 1, 2009 9:29 AM

pons_saravanan said:

excellant, thats why i love VB over other languages. some more improvements to come in.

# November 11, 2009 4:47 AM