Which Problems Are Not Detected?

Which Problems Are Not Detected?

Perhaps you re asking yourself, Which errors doesn t the Upgrade Wizard catch? The Upgrade Wizard doesn t warn you about five important errors:

  • The wizard doesn t warn about the New statement. The following code has a different behavior in Visual Basic 6 than in Visual Basic .NET (see Chapter 1for a full explanation):

    Dim x As New y

    The wizard doesn t output an EWI for this because in 99.99 percent of the cases you won t notice the difference. If the wizard did create a warning for every occurrence, some projects would be overcome with EWIs.

  • The wizard doesn t detect all coercion problems (in which a variable of one type is assigned to another type). Some coercions are allowed in Visual Basic 6 but not in Visual Basic .NET. Generally these errors are easy to fix, since they almost always result in compile errors.

  • It is common to pass variables as parameters to functions. Often these parameters are defined as ByRef, which means that if the function changes the value of the parameter, the underlying variable also changes. For example, in the following code snippet, myVariable is passed to a function as myParameter, and the function changes myParameter to 5, which also changes myVariable to 5, since it is passed as a ByRef parameter.

    Sub Main()    Dim myVariable As Integer    myFunction myVariable    MsgBox myVariable End Sub Function myFunction(myParameter As Integer)    myParameter = 5 End Function

    Visual Basic 6 has one exception to this behavior: if you pass a property as a ByRef parameter, it will not be changed. For example, suppose that our application has a form with a TextBox on it called Text1. The following line of code passes the Text property of Text1 to the function myFunction.

    myFunction Form1.Text1.Text

    In Visual Basic 6, because the Text property is a property, it is not changed when the function explicitly changes the value. In effect, properties are always passed ByVal. Visual Basic .NET does not have this limitation; properties passed as ByRef parameters are treated as ByRef parameters. If the example just given was upgraded to Visual Basic .NET, the Text property would be changed.

    In some cases, this may lead to subtle differences in your project s run-time behavior. You can force Visual Basic .NET to pass properties as ByVal parameters by adding parentheses around the variable. For example, this code passes the Text property ByRef:

    myFunction(Me.Text1.Text)

    whereas this code passes the Text property ByVal (just as Visual Basic 6 did):

    myFunction((Me.Text1.Text))

  • In Visual Basic 6, you can use ReDim to redimension a variable to have a different number of indexes. For example, the following code is valid in Visual Basic 6 but causes a compile error in Visual Basic .NET:

    Dim x() ReDim x(5, 5)

    The Upgrade Wizard doesn t warn you about this, but the compile error makes it easy to detect and easy to fix. In Visual Basic .NET, each time a variable is redimensioned, it must always have the same number of indexes. In Visual Basic .NET, if you change the Dim statement to dimension the variable with two indexes (using a comma), it works perfectly:

    Dim x(,) ReDim x(5, 5)

  • The wizard warns you about problems it detects with ActiveX controls. However, there are subtle differences between hosting ActiveX controls in Visual Basic 6 and hosting them in Visual Basic .NET. The wizard doesn t know about all these differences, and therefore it can t warn you. For a discussion of using ActiveX controls in Visual Basic .NET, see Chapter 9 and Chapter 13.

It s important to be conscious of these issues. Learn the methods that will allow you to handle each of them just as you handle issues that the Upgrade Wizard detects.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net