Upgrade Recommendation: Use Early Binding and Explicit Conversions
Both Visual Basic 6.0 and Visual Basic 2005 support using late-bound objects, which is the practice of declaring a variable as the Object data type and assigning it to an instance of a class at run time. However, during the upgrade process, late-bound objects can introduce problems when resolving default properties, or in cases where the underlying object model has changed and properties, methods, and events need to be converted. For example, suppose you have a form called Form1 with a label called Label1; the following Visual Basic 6.0 code would set the caption of the label to "SomeText":
Dim o As Object Set o = Me.Label1 o.Caption = "SomeText"
In Visual Basic 2005, the Caption property of a label control is replaced by theproperty. When your code is upgraded, all instances of the Caption property are changed to Text, but because a late-bound object is typeless, Visual Basic cannot detect what type of object it is, or if any properties should be translated. In such cases, you will need to change the code yourself after upgrading.
If you rewrite the code using early-bound objects, it will be upgraded automatically:
Dim o As Label Set o = Me.Label1 o.Text = "SomeText"
Where possible, you should declare variables of the appropriate object type rather than simply declaring them as the Object data type.
In the cases where you do use Object and Variant variables in your Visual Basic 6.0 code, we recommend you use explicit conversions when you assign the variables, perform operations on the variables, or pass the variables to a function. For example, the intention of the '+' operation in the following code is unclear:
Dim Var1 As Variant Dim Var2 As Variant Dim Var3 As Variant Var1 = "3" Var2 = 4 'BAD: Should Var1 and Var2 be added as strings or integers? Var3 = Var1 + Var2
The above example may result in a run-time error in Visual Basic 2005. Rewriting the final line to use explicit conversions ensures the code will work:
'GOOD: explicit conversion Var3 = CInt(Var1) + CInt(Var2)
Visual Basic 2005 supports overloading functions based on parameter type. For example, the Environ function now has two forms:
Environ( Expression As Integer) As String Environ( Expression As String ) As String
Visual Basic 2005 determines which function to call based on the parameter type. If you pass an integer to Environ(), the integer version is called; if you pass a string, then the string version is called. Code that passes a Variant or Object data type to an overloaded function may cause a compiler error or run-time error. Using an explicit conversion, as in the following example, will mean your code will work as intended after it is upgraded to Visual Basic 2005:
Dim a As String Dim v As Variant v = "Path" 'GOOD: explicit conversion a = Environ(CStr(v))
Using explicit conversions of late-bound objects is good coding practice. It makes the intention of the code easy to determine, and makes it easier for you to upgrade your project to Visual Basic 2005.