Visual Basic .NET and Visual Basic 2005 do not support default properties (311328)



The information in this article applies to:

  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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="Important"
   L.Caption="Important"
				
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 "Important" 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="Saving"  '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="Saving"     '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.

REFERENCES

For more information about default properties that take arguments, refer to the "Visual Basic Language Concepts" topic under "Default Property Changes" in the Visual Basic Help documentation.

Modification Type:MajorLast Reviewed:1/27/2006
Keywords:kbvs2005swept kbvs2005applies kbinfo KB311328