This documentation is archived and is not being maintained.

Default Property Changes for Visual Basic 6.0 Users 

Visual Basic 2005 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 lbl As Label 
lbl = "Important" 
lbl.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 lbl 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 lbl1 As Label, lbl2 As Label 
    lbl1 = "Saving" ' Assign a value to lbl1's Caption property. 
    lbl2 = lbl1       ' Replace lbl2's Caption property with lbl1's. 
    Set lbl2 = lbl1   ' Replace lbl2 with an object reference to lbl1. 

Visual Basic 2005

In Visual Basic 2005, 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 New Label   ' Both become type Label
                          ' in the new version of Visual Basic.
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 2005, even though it has no syntactical use. This helps avoid confusion with its former meanings. Visual Basic 2005 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 2005. 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 Controls property returns a ControlCollection object, and the Item property returns a Control object. The following example shows both valid and invalid use of default properties in Visual Basic 2005.

Dim F As New 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 2005, 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.

See Also