Errors, Warnings, and Issues

Chapter 8

Errors, Warnings, and Issues

This chapter looks at the different errors, warnings, and issues the Upgrade Wizard generates when you upgrade your Visual Basic 6 projects to Visual Basic .NET.

Each time you upgrade a project, the wizard upgrades most of the code and objects to Visual Basic .NET, but some items are not automatically changed. These items require manual modifications after the wizard finishes. To understand why, consider this example. In Visual Basic .NET, the Caption property of a label is now called the Text property. Label.Caption maps to Label.Text. The Upgrade Wizard knows about this and changes all Label.Caption references to Label.Text. But what happens if the wizard can t determine whether a particular object is indeed a label? When the wizard isn t able to determine what data type an object is, it inserts a warning into your code. The following code is a good example. Assume Form1 is a form, and Label1 is a Label control:

Dim o As Variant Set o = Form1.Label1 o.Caption = "VB Rocks"

In this example, the variable o is declared as a variant and then assigned to Label1. The Caption property is then set to the string VB Rocks. Because o is a variant and is late-bound, Visual Basic 6 doesn t know what type of object it is at design time. (This is why you don t get IntelliSense for variants at design time.) Like the Visual Basic 6 Code Editor, the Upgrade Wizard also has a design-time view of the code. We ll follow what the wizard does as it walks through the three lines of code in our simple example:

Dim o As Variant

The first line is easy; the wizard knows to change Variant data types to Object, so it upgrades the first line as follows:

Dim o As Object

The second line is also upgraded automatically:

Set o = Form1.Label1

This is a Set statement, so it has to be an object assignment rather than a default property assignment. The variable o is assigned to the control Form1.Label1 and upgraded as follows:

o = Form1.DefInstance.Label1

The first two lines were upgraded automatically. The final line, however, causes problems:

o.Caption = "VB Rocks"

Because o is late-bound, the Upgrade Wizard doesn t know what to do with the Caption property. Perhaps o is a label, but perhaps it is another type of control. When the Upgrade Wizard finds code that can t be resolved at design time, it inserts a warning. The final line upgrades to the following:

'UPGRADE_WARNING: Couldn't resolve default property of object o. Caption. 'Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup1037.htm

o.Caption = "VB Rocks"

What do you do now? If you run the code, it causes a run-time exception. Often, the best solution is to change the variable declaration to be strongly typed. In this example, change the first line to

Dim o As Label

After this change, the compiler will report that Caption is not a property of Label by generating a Task List entry and by highlighting the code with a green underline. You should then change the property to Text. Another good solution that avoids the problem entirely is to declare o as a label in Visual Basic 6. Declaring it as a label makes it strongly typed, and the wizard will have no trouble changing Caption to Text. Here is how to rewrite the code in Visual Basic 6 to be strongly typed:

Dim o As Label Set o = Form1.Label1 o.Caption = "VB Rocks"

After upgrading, the code becomes

Dim o As System.Windows.Forms.Label o = Form1.DefInstance.Label1 o.Text = "VB Rocks"

This code works perfectly in Visual Basic .NET.

This is a simple example; we can figure out from looking at the code that o is a label. However, the wizard doesn t know this. It has a set of rules for converting Visual Basic 6 to Visual Basic .NET that it follows for each property, method, and event of a particular object type. When a variable is late-bound, the wizard doesn t know how to interpret its properties.

Couldn t the Upgrade Wizard simply guess the object type? Other examples show the futility of this strategy. In the following example, even the original author of the code wouldn t be able to determine what type of object o will be set to at run time. Let s assume that Label1 is a label and Text1 is a text box:

Dim o As Object If someVariable = 1 Then    Set o = Me.Text1 Else    Set o = Me.Label1 End If

It s impossible to know whether o will be a label or a text box until the code actually executes.

EWI is the common term for the errors, warnings, and issues the Upgrade Wizard inserts into your code. EWIs alert you to problems like the preceding one. EWI comments are inserted on the line before the problem. The comment text is in two parts: the message and the link to Help. In the following code, the underlined part of the text is actually a hyperlink to the appropriate Help topic:

'UPGRADE_WARNING: Couldn't resolve default property of object o. Caption. 'Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup1037.htm

All EWIs are associated with Help topics that show you how to fix the problem. Because the EWI is inserted as a comment, it doesn t affect the compilation or execution of your project.

Identifying All the Problems

Does the Upgrade Wizard detect every problem during an upgrade? Is every problem marked in your code with easy-to-follow guidance? Unfortunately, it s not quite as simple as that. Some problems might have only a 0.01 percent chance of occurring, so what should the wizard do mark every occurrence of the code, generating false alarms in 99.99 percent of the cases, or ignore a potential problem and let it generate a compile or run-time error? The Upgrade Wizard walks a fine line in determining what it should treat as an error and what it shouldn t. It warns about most problems it finds, but as you ll see later in this chapter, there are some that it doesn t warn about.



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