6.3. Upgrading VB 6 Applications


To make it easier for you to upgrade your VB 6 applications, Microsoft provides made two free tools:


Code Advisor for Visual Basic 6.0

An add-on for Visual Studio 6.0 that reviews your existing VB 6 code to ensure it meets predetermined coding standards.


Upgrade Wizard

A built-in Visual Studio 2005 tool that automatically upgrades your VB 6 applications when you open them in Visual Studio 2005. The Upgrade Wizard will upgrade your VB 6 code to VB 2005 and flag those code blocks that need further attention.

Before you upgrade your VB 6 application, you should first run your application through the Code Advisor for VB 6 and fix any ambiguous code it identifies that will possibly make the upgrade process difficult. Then, open your VB 6 application in Visual Studio 2005 so that the Upgrade Wizard can upgrade your code to VB 2005.

6.3.1. Using Code Advisor for VB 6

The first step toward upgrading your VB 6 applications to VB 2005 is to run the Code Advisor for Visual Basic 6.0. The Code Advisor for Visual Basic 6 is an add-on for Visual Studio 6.0 that is used to review your code to ensure that it meets predetermined coding standards. The coding standards are based on best practices developed by Microsoft to produce robust and easy-to-maintain code. You can download this free tool from: http://www.microsoft.com/downloads/details.aspx?FamilyID=a656371a-b5c0-4d40-b015-0caa02634fae&DisplayLang=en.

Once the Code Advisor for VB 6 is downloaded and installed, you will find a new set of buttons in the toolbar area of Visual Studio 6.0, as shown in Figure 6-8.

The Scope Definition button allows you to use Code Advisor to check the entire project, or to check only the currently active file.

To see how the Code Advisor for VB 6 works, consider the following simple VB 6 application consisting of a single form (Hello Application) as shown in Figure 6-9. The form contains four controls, including a Label control (lblMessage), a Text control (txtName), a Hello button (cmdHello), and an Exit button (cmdExit).

Figure 6-8. The Code Advisor toolbar


Figure 6-9. An application in VB 6


When the Exit button is clicked, the form displays a message box that asks users if they want to exit, as shown in Example 6-2.

Example 6-2. Exit button Click event handler
 Private Sub cmdExit_Click() response = MsgBox("Exit program?", vbYesNo) If response = vbYes Then End End If End Sub 

This VB 6 application also uses a subroutine to display a message via the MsgBox function, as shown in Example 6-3. Note that this function uses the default ByRef to pass in parameters by reference.

Example 6-3. DisplayMsg subroutine
 Public Sub DisplayMsg(str As String) MsgBox str End Sub 

When the Hello button is clicked, the TextBox control is assigned to an Object of type object. A comparison is then performed to check if the Text property (not explicitly specified as it is the default property) in the TextBox control is empty. The relevant message is then printed. The code is shown in Example 6-4.

Example 6-4. Hello button Click event handler
 Private Sub cmdHello_Click() Dim obj As Object Set obj = txtName If obj = "" Then DisplayMsg ("Please enter your name") Else DisplayMsg ("Hello " & txtName) End If End Sub 

To use the Code Advisor to examine the application, click the Add FixIts button. The toolbar will display the number of issues (known as FixIts) that Code Advisor has raised (in this example, two FixIts were raised Count:2), as shown in Figure 6-8.

To examine the FixIts raised, switch to the Code view where you will see comments prefixed with the word "FIXIT" as shown in Example 6-5.

Example 6-5. Hello and Exit button Click event handler FIXITs
 'FIXIT: Use Option Explicit to avoid implicitly ' creating variables of type Variant ' FixIT90210ae-R383-H1984 Private Sub cmdExit_Click() response = MsgBox("Exit program?", vbYesNo) If response = vbYes Then End End If End Sub Public Sub DisplayMsg(str As String) MsgBox str End Sub Private Sub cmdHello_Click() 'FIXIT: Declare 'obj' with an early-bound data type 'FixIT90210ae-R1672-R1B8ZE Dim obj As Object Set obj = txtName If obj = "" Then DisplayMsg ("Please enter your name") Else DisplayMsg ("Hello " & txtName) End If End Sub 

Using the Code Advisor toolbar, you can remove all FixIts by clicking the Remove FixIts button, or find the next FixIts by clicking the Find Next FixIts button.

In this example, the two specific FixIts are:

  • You should use Option Explicit to prevent your code from using undeclared variables. Once this is done, the response variable should be explicitly declared.

  • The obj variable should be declared as a TextBox for early binding.

To view a summary of the FixIts raised, you can click on the View FixIt Report button. The report is shown as a web page (see Figure 6-10).

Figure 6-10. Viewing the FixIts report


You can customize the Code Advisor (click on Filter FixIts Rules) to examine your code using a specific version of Visual Basic (Visual Basic .NET 2002 or Visual Basic.NET 2003), or based on best practices (see Figure 6-11).

Figure 6-11. Customizing Code Advisor


Let's now modify the application by entering to code shown in bold in Example 6-6 and then run Code Advisor again (by clicking on the Add FixIts button).

Example 6-6. Modified Hello and Exit button Click event handlers
 Option Explicit Private Sub cmdExit_Click() Dim response As VbMsgBoxResult response = MsgBox("Exit program?", vbYesNo) If response = vbYes Then End End If End Sub Public Sub DisplayMsg(str As String) MsgBox str End Sub Private Sub cmdHello_Click() Dim obj As TextBox Set obj = txtName If obj = "" Then DisplayMsg ("Please enter your name") Else DisplayMsg ("Hello " & txtName) End If End Sub 

This time, the application passes the Code Advisor's check. You can now proceed to the next step of the upgrading process: using the Upgrade Wizard to upgrade the code to VB 2005.

6.3.2. Using the Upgrade Wizard

When you try to open a Visual Basic project (Windows, web, or other) created with a previous version of Visual Studio (Visual Studio 6 or Visual Studio .NET), Visual Studio 2005 will launch the Upgrade Wizard and attempt to upgrade the project to VB 2005. The Upgrade Wizard will automatically upgrade your code to VB 2005, and anything else that is ambiguous will be marked with comments and displayed in the Upgrade Report.

To see how the Upgrade Wizard works, let's upgrade the application discussed in "Using Code Advisor for VB 6." You'll use Visual Studio 2005 to open this VB project. When it's opened in Visual Studio 2005, the Upgrade Wizard will kick into action, as shown in Figure 6-12.

Figure 6-12. The Visual Basic Upgrade Wizard


The Upgrade Wizard will lead you through a series of steps to upgrade the VB 6 project. When you have finished, your VB 6 project will be displayed in Visual Studio 2005.

Figure 6-13 shows the project after it has been upgraded to VB 2005.

Figure 6-13. The upgraded VB project


In Solution Explorer, note that a new item has been added to the project: _UpgradeReport.htm. The _UpgradeReport.htm document lists the changes made to the original project and highlights the various issues encountered during the upgrade process, as shown in Figure 6-14.

Figure 6-14. The content of the _UpgradeReport.htm file


Let's look at the upgraded code and observe some of the salient changes, as shown in Example 6-7. The comments and code added by the Upgrade Wizard are highlighted in bold.

Example 6-7. The upgraded project
 Option Strict Off Option Explicit On Friend Class Form1 Inherits System.Windows.Forms.Form Private Sub cmdExit_Click( _ ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) _ Handles cmdExit.Click  Dim response As MsgBoxResult  response = MsgBox("Exit program?", MsgBoxStyle.YesNo)  If response = MsgBoxResult.Yes Then  End  End If End Sub 'UPGRADE_NOTE: str was upgraded to str_Renamed. 'Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/ 'redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"' Public Sub DisplayMsg(ByRef str_Renamed As String) MsgBox(str_Renamed) End Sub Private Sub cmdHello_Click( _ ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) _ Handles cmdHello.Click  Dim obj As System.Windows.Forms.TextBox  obj = txtName  If obj.Text = "" Then DisplayMsg(("Please enter your name"))  Else DisplayMsg(("Hello " & txtName.Text))  End If End Sub End Class 

Note that while there is only one upgrade note in this project, several changes have been made to the code:

  • The variable str in the DisplayMsg subroutine has been renamed and the ByRef keyword has been inserted. You should always explicitly specify the ByRef or ByVal keyword before you upgrade so that you don't get unexpected results.

  • The Option Strict Off statement is inserted into the code. As the Option Strict statement is not supported in VB 6, it is turned off by default in VB 2005. Ideally, you should turn it on so that all narrowing conversions are flagged (see Chapter 2 for details).

  • The constants vbYesNo and vbYes have been changed to MsgBoxStyle. YesNo and MsgBoxResult.Yes, respectively.

  • The Text property has been explicitly added to the obj and txtName variables.

What you have seen here is a superficial view of the support the Visual Basic Upgrade Wizard can provide. For more details, check out this article at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vstchexpvsnetlab5.asp.

Note that the Visual Basic Upgrade Wizard can only upgrade applications written in VB 6 and later. As such, if you want to upgrade applications written in a version of Visual Basic released prior to VB 6, you need to first upgrade them to VB 6 before using the Upgrade Wizard.




Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

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