Monday, April 19, 2010

Auto-Implemented Properties in VB.Net

 

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. When you write code for an auto-implemented property, the Visual Basic compiler automatically creates a private field to store the property variable in addition to creating the associated Get and Set procedures.

With auto-implemented properties, a property, including a default value, can be declared in a single line. The following example shows three property declarations.

 

Public Property Name As String
Public Property Owner As String = "DefaultName"
Public Property Items As New List(Of String) From {"M", "T", "W"}
Public Property ID As New Guid()

An auto-implemented property is equivalent to a property for which the property value is stored in a private field. The following code example shows an auto-implemented property.

 

Property Prop2 As String = "Empty"

The following code example shows the equivalent code for the previous auto-implemented property example.

 

Private _Prop2 As String = "Empty"
Property Prop2 As String
Get
Return _Prop2
End Get
Set(ByVal value As String)
_Prop2 = value
End Set
End Property

Backing Field






When you declare an auto-implemented property, Visual Basic automatically creates a hidden private field called the backing field to contain the property value. The backing field name is the auto-implemented property name preceded by an underscore (_). For example, if you declare an auto-implemented property named ID, the backing field is named _ID. If you include a member of your class that is also named _ID, you produce a naming conflict and Visual Basic reports a compiler error.

The backing field also has the following characteristics:



  • The access modifier for the backing field is always Private, even when the property itself has a different access level, such as Public.



  • If the property is marked as Shared, the backing field also is shared.



  • Attributes specified for the property do not apply to the backing field.



  • The backing field can be accessed from code within the class and from debugging tools such as the Watch window. However, the backing field does not show in an IntelliSense word completion list.


Initializing an Auto-Implemented Property






Any expression that can be used to initialize a field is valid for initializing an auto-implemented property. When you initialize an auto-implemented property, the expression is evaluated and passed to the Set procedure for the property. The following code examples show some auto-implemented properties that include initial values.

 

Property FirstName As String = "James"
Property PartNo As Integer = 44302
Property Orders As New List(Of Order)(500)

You cannot initialize an auto-implemented property that is a member of an Interface, or one that is marked MustOverride.

When you declare an auto-implemented property as a member of a Structure, you can only initialize the auto-implemented property if it is marked as Shared.

When you declare an auto-implemented property as an array, you cannot specify explicit array bounds. However, you can supply a value by using an array initializer, as shown in the following examples.

 

Property Grades As Integer() = {90, 73}
Property Temperatures As Integer() = New Integer() {68, 54, 71}

No comments:

Post a Comment