One more VB 2010 Language Feature....

Happy to see the interest in last weeks post!  I'd like to let all the VB-ers know of another very nice addition to Microsoft's BASIC language, the use of automatic properties. As you are aware, under .NET, the perferred way to encapsulate simple state data is via type properties. Much like a normal get/set method pair, .NET properties will allow the object user to safely interact with private data.  However, when doing so, it appears they are working on a public field (from a syntatic point of view). 

Before the release of .NET 4.0, VB programmers would build properties using code such as the following:

Public Class Animal

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

End Class

While the previous property definition is fine, it is a tad bit verbose, especially when all you want to do is have basic encapsulation (in other words, no checking against business rules, no error logging, etc). To simplify the amount of typing time, VB 2010 now allows you to define a property as so:

Public Class Animal
      Public Property Name() As String = ""
End Class

When processed by the compiler, it will automatically create a private backing field, and implement the get/set logic. Understand of course if you *do* require properties which do more than simply get/set a value, you will want to fall back on traditional syntax.


Posted by: Andrew Troelsen
Posted on: 4/15/2010 at 1:02 PM
Tags:
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading


Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.