Compartilhar via


Auto-Implemented Properties (Visual Basic)

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}

Property Definitions That Require Standard Syntax

Auto-implemented properties are convenient and support many programming scenarios. However, there are situations in which you cannot use an auto-implemented property and must instead use standard, or expanded, property syntax.

You have to use expanded property-definition syntax if you want to do any one of the following:

  • Add code to the Get or Set procedure of a property, such as code to validate incoming values in the Set procedure. For example, you might want to verify that a string that represents a telephone number contains the required number of numerals before setting the property value.

  • Specify different accessibility for the Get and Set procedure. For example, you might want to make the Set procedure Private and the Get procedure Public.

  • Create properties that are WriteOnly or ReadOnly.

  • Use parameterized properties (including Default properties). You must declare an expanded property in order to specify a parameter for the property, or to specify additional parameters for the Set procedure.

  • Place an attribute on the backing field, or change the access level of the backing field.

  • Provide XML comments for the backing field.

Expanding an Auto-Implemented Property

If you have to convert an auto-implemented property to an expanded property that contains a Get or Set procedure, the Visual Basic Code Editor can automatically generate the Get and Set procedures and End Property statement for the property. The code is generated if you put the cursor on a blank line following the Property statement, type a G (for Get) or an S (for Set) and press ENTER. The Visual Basic Code Editor automatically generates the Get or Set procedure for read-only and write-only properties when you press ENTER at the end of a Property statement.

See Also

Tasks

How to: Declare and Call a Default Property in Visual Basic

How to: Declare a Property with Mixed Access Levels (Visual Basic)

Reference

Property Statement

ReadOnly (Visual Basic)

WriteOnly (Visual Basic)

Other Resources

Objects and Classes in Visual Basic