Microsoft KB Archive/311328

= Visual Basic .NET and Visual Basic 2005 do not support default properties =

Article ID: 311328

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311328



SUMMARY
Visual Basic .NET and Visual Basic 2005 do not support default properties except in special circumstances when the property has arguments. Visual Basic .NET and Visual Basic 2005 update default property support for simplification and improved readability.



MORE INFORMATION
Microsoft Visual Basic 6.0 supports default properties. For example, a Label control has the default Caption property. The following two assignment statements are equal: Dim L as Label L=&quot;Important&quot; L.Caption=&quot;Important&quot; Although default properties enable you to write Visual Basic 6.0 code with some shorthand, default properties also include the following disadvantages:  Default properties can make code more difficult to read. In the preceding example, if you are not familiar with the Label control, you cannot discern whether the string &quot;Important&quot; in the first assignment is stored directly in the variable L or in a default property. If you use an object in your code, you cannot easily determine whether the object has a default property. In addition, if the object has a default property, you cannot easily discern which property is the default.  With default properties, you must use the Set statement in the Visual Basic language. The following example illustrates how you must use Set to assign an object reference rather than a default property: Dim L1 as Label Dim L2 as Label L1=&quot;Saving&quot; 'Assign a value to the caption property of L1. L2=L1       'Replace the caption property of L2 with the caption property of L1. Set L2=L1   'Replace L2 with an object reference to L1. 

Visual Basic .NET and Visual Basic 2005 do not support default properties unless the default properties take arguments. Because of this change in syntax, you do not have to use the Let and the Set statements to specify the assignment. Furthermore, Let and Set are not used in assignment statements.

The Text property replaces the Caption property on the Label control. Therefore, you can rewrite the preceding example as follows: Dim L1, L2 as Label 'Both L1 and L2 are type Label in Visual Basic .NET. L1.Text=&quot;Saving&quot;    'Assign the Text property of L1. L2.Text=L1.Text     'Copy the Text property of L1 to the Text property of L2. L2=L1               'Copy the L1 Label object to the L2 Label object. The Let statement is still a reserved word in Visual Basic .NET and in Visual Basic 2005, even though it has no use in syntax. This helps to avoid confusion with the former meanings of Let. Visual Basic .NET and Visual Basic 2005 use the Set statement for property procedures that set the value of the property.

