Upgrade Recommendation: Resolve Parameterless Default Properties

In Visual Basic 6.0, many objects expose default properties, which can be omitted as a programming shortcut. For example, TextBox has a default property of Text, so instead of writing:

MsgBox Form1.Text1.Text

you use the shortcut:

MsgBox Form1.Text1

The default property is resolved when the code is compiled. In addition, you can also use default properties with late-bound objects, as in the following example:

Dim obj As Object
Set obj = Form1.Text1
MsgBox obj

In the late-bound example, the default property is resolved at run time, and the MsgBox displays the value of the default property of the TextBox as Text1.

Visual Basic 2008 does not support parameterless default properties, and consequently does not allow this programming shortcut. When your project is upgraded, Visual Basic 2008 resolves the parameterless default properties, but late-bound usages that rely on run-time resolution cannot be automatically resolved. In these cases, you will have to change the code yourself. An additional complication is that many libraries implement default properties using a property called _Default. _Default acts as a proxy, passing calls to the real default property. So, when your project is upgraded, some default properties will be resolved to _Default. The code will still work as usual, but it will be less understandable than code written explicitly using the actual property. For these reasons, try to avoid using parameterless default properties in your Visual Basic 6.0 code. Instead of writing:

Dim obj As Object
Set obj = Me.Text1
'BAD: Relying on default property
MsgBox obj
'BAD: Relying on default property
MsgBox Me.Text1

use:

Dim obj As TextBox
Set obj = Me.Text1
'GOOD: Default property is resolved
MsgBox obj.Text
'GOOD: Default property is resolved
MsgBox Me.Text1.Text

While parameterless default properties are not supported in Visual Basic 2008, default properties with parameters are supported. To understand the difference between the two types, consider that parameterized default properties always have an index. An example is the default property of ADO recordset: the Fields collection. The code:

Dim rs As ADODB.Recordset
rs("CompanyName") = "SomeCompany"
rs!CompanyName = "SomeCompany"

is actually a shortcut for:

Dim rs As ADODB.Recordset
rs.Fields("CompanyName").Value = "SomeCompany"
rs.Fields!CompanyName.Value = "SomeCompany"

In this case, the Fields property is parameterized, and so the usage is valid in Visual Basic 2008; however, the default property of the Fields property, Value, is parameterless, so the correct usage in Visual Basic 2008 is:

Dim rs As ADODB.Recordset
rs("CompanyName").Value = "SomeCompany"
rs!CompanyName.Value = "SomeCompany"

This example and most other default properties are resolved for you when the project is upgraded, so resolving them in Visual Basic 6.0 is simply a good programming practice. However, you should avoid using default properties with the Object and Variant data types, as these cannot be resolved and you will have to fix the code yourself in the upgraded project.

See Also

Other Resources

Language Recommendations for Upgrading