Default Property Changes in Visual Basic



Default Property Changes in Visual Basic

 
Visual Basic .NET updates default property support for simplification and improved readability.

Visual Basic 6.0

In Visual Basic 6.0, default properties are supported on objects. On a Label control, for example, Caption is the default property, and the two assignments in the following example are equivalent:

        Dim L As Label
        L = "Important"
        L.Caption = "Important"

While default properties enable a certain amount of shorthand in writing Visual Basic code, they have several drawbacks:

  • They can make code more difficult to read. In the preceding example, if you are not familiar with the Label control, you cannot tell from the first assignment whether the string "Important" is being stored directly in the variable L or in a default property.
  • Given an object that you plan to use in your code, it is not always easy to discover whether it has a default property, and if so, which property that is.
  • Default properties make the Set statement necessary in the Visual Basic language. The following example shows how Set is needed to indicate that an object reference, rather than a default property, is to be assigned:

            Dim L1 As Label, L2 As Label
            L1 = "Saving"  ' Assign a value to L1's Caption property.
            L2 = L1        ' Replace L2's Caption property with L1's.
            L2 = L1    ' Replace L2 with an object reference to L1.     End Sub

Visual Basic .NET

In Visual Basic .NET, default properties are not supported unless they take arguments. Because of this syntax change, the Let and Set statements are not needed to specify what is to be assigned, and they are not used in assignment statements. The Text property replaces the Caption property on the Label control, and the preceding example can be rewritten as follows:

        Dim L1, L2 As Label   ' Both become type Label in Visual Basic .NET.
        L1.Text = "Saving"    ' Assign Text property.
        L2.Text = L1.Text     ' Copy Text property.
        L2 = L1               ' Copy object reference.

Let is still a reserved word in Visual Basic .NET, even though it has no syntactical use. This helps avoid confusion with its former meanings. Visual Basic .NET uses the Set statement for property procedures that set the value of a property.

Parameterized Properties

Default properties that take arguments are not ambiguous, and they are supported in Visual Basic .NET. Default properties appear most commonly on collection classes. In the System.Windows.Forms namespace, for example, the Form class supports the following hierarchy:

Form object

   Controls property   (returns a ControlCollection object for this form)

      ControlCollection object   (default property is Item)

         Item property   (returns a Control object for one item in the collection)

            Control object

The Form.Controls property returns a ControlCollection object, and the ControlCollection.Item property returns a Control object. The following example shows both valid and invalid use of default properties in Visual Basic .NET:

        Dim F As Form   ' Assume F has been created and initialized.
        ' ...
        F.Controls.Item(0).Text = "Stop"   ' Valid -- no default properties used.
        F.Controls(0).Text = "Stop"   ' Valid -- Item is parameterized.
        ' ...
        F(0).Text = "Stop"   ' INVALID -- Form does not have a default property.
        F.Controls(0) = "Stop"   ' INVALID -- No default property on Control.

Default Property Declaration

In Visual Basic .NET, you specify a property as the default property by beginning its declaration with the Default keyword. If you overload the property name, you must specify Default in every overload declaration. You cannot declare a default property to be Shared or Private.