Default Property Changes for Visual Basic 6.0 Users
Visual Basic 2008 updates default property support for simplification and improved readability.
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.
In Visual Basic 2008, 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.
Let is still a reserved word in Visual Basic 2008, even though it has no syntactical use. This helps avoid confusion with its former meanings. Visual Basic 2008 uses the Set statement for property procedures that set the value of a property.
Default properties that take arguments are not ambiguous, and they are supported in Visual Basic 2008. Default properties appear most commonly on collection classes. In the System.Windows.Forms namespace, for example, the Form class supports the following hierarchy:
The Controls property returns a Control.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 2008.