Upgrade Walkthrough

Upgrade Walkthrough

Let s start by creating a simple project in Visual Basic 6. We will assume that you have Visual Basic 6 and Visual Basic .NET installed on the same machine. Having both versions installed is actually a recommended configuration. Visual Basic .NET installs to a different directory than Visual Basic 6. If Visual Basic 6 is already installed, it is left intact. Visual Basic .NET does not overwrite any Visual Basic 6 files the two versions can be installed and run side by side. The order of installation doesn t matter either; you can set up Visual Basic 6 after installing Visual Basic .NET or vice versa. The benefits of installing both products on the same machine are twofold:

  • It allows you to ensure that your project actually runs. (If the project doesn t work in Visual Basic 6, it s unlikely that it will work after upgrading.)

  • Visual Basic 6 Setup installs common ActiveX controls and libraries. These are not distributed with Visual Basic .NET. The easiest way to get them on the machine is to install Visual Basic 6.

We are going to create a simple Visual Basic 6 project, prjFirstUpgrade, and upgrade it to Visual Basic .NET. (You can find a completed version of this project on the companion CD that accompanies this book.) The project we are about to develop generates a new random number between 0 and 10 every second and shows the number in a TextBox. It continues to do this until you click a CommandButton to stop it. Let s get started. Launch Visual Basic 6, and follow these steps to create the project:

  1. Create a new Standard EXE project.

  2. After Visual Basic 6 creates the project, you ll see that Form1 is displayed in the integrated development environment (IDE). Add a TextBox, a CommandButton, and a Timer control to Form1.

  3. Select the Timercontrol. In the Properties grid, set the interval to 1000.

  4. Double-click the Command1 CommandButton, and add this code to the Command1_Click procedure:

    Private Sub Command1_Click()    Me.Timer1.Interval = 0 End Sub

  5. Now return to the Form window, and double-click the Timercontrol to create the Timer1_Timer event. Add the following code to the event:

    Private Sub Timer1_Timer()    Dim i As Integer    i = Rnd * 10        Me.Text1 = i End Sub

  6. Save the project, using the default name for the form: Form1. We ll name the project something meaningful: prjFirstUpgrade. Make sure you save the project into a directory on your local computer, rather than to a network share. This step ensures that Visual Basic .NET has enough security permissions to upgrade and run the project. The form and code should look similar to Figure 5-1.

    Figure 5-1

    Random number generator in Visual Basic 6.

When you run the project, you ll see that the TextBox updates every second with a new random number. It stops when you click Command1.

Now that we ve created a Visual Basic 6 project, let s upgrade it to Visual Basic .NET. You can leave Visual Basic 6 running, with the project still loaded doing so doesn t interfere with the upgrade. Start Visual Basic .NET, and follow these steps to upgrade your project:

  1. In Visual Basic .NET, from the File menu, choose Open and then Project to display the Open Project dialog box. Navigate to the location where you saved your Visual Basic 6 project file, select prjFirstUpgrade.vbp, and click the Open button to upgrade prjFirstUpgrade using the Upgrade Wizard.

  2. The first page of the wizard contains welcome information, as shown in Figure 5-2. After you ve read the welcome information, click Next to move to the second page.

    Figure 5-2

    Welcome screen of the Upgrade Wizard.

  3. The second page, shown in Figure 5-3, is where you set options for the upgrade. These options may be disabled depending on the project type, and in most cases, you will leave them at their default setting, but let s examine them more closely to see when you would use them. There are two options. The first determines what type of Visual Basic .NET project to upgrade to. As you would expect, EXEs upgrade to EXEs, and DLLs upgrade to DLLs; for standard EXE and DLL projects, the upgrade type is determined automatically. With EXE server projects, however, you have some flexibility. These projects act like a combination of an EXE and a DLL. When upgrading these projects, you have the choice of converting them to a Visual Basic .NET EXE or to a Visual Basic .NET DLL. Since our project is a standard EXE, the first choice (EXE) is automatically selected. The second choice, DLL/Custom Control Library, is disabled, and for this reason the second option, Generate Default Interfaces For All Public Classes, is disabled also.

    This option is available only for DLL and EXE server projects. You should select this option only if other projects use the Implements keyword to implement classes from within your DLL or EXE server. Selecting this option causes the wizard to create an interface for every public class in your project.

    Click Next to move to page 3.

    Figure 5-3

    Choosing a project type.

  4. On the third page, shown in Figure 5-4, you specify the destination directory for the upgraded project. The project is not moved to this location. Instead, the Upgrade Wizard leaves your Visual Basic 6 project untouched and creates a brand new Visual Basic .NET project in the directory you specify here. By default, the wizard suggests placing the upgraded project in a new directory called <projectname>.NET. You can use the text box and the Browse button to change the location of this directory, but in most cases you should simply accept the default name. You can always move the directory later simply by copying it to a different location.

    Figure 5-4

    Specifying a destination directory.

    If the destination directory already contains files, these will be deleted to make room for the new project. With our soon-to-be-upgraded Visual Basic 6 project, let s accept the suggested name, prjFirstUpgrade.NET. Click Next to move to page 4.

  5. Because the prjFirstUpgrade.NET folder doesn t exist yet, a warning message appears asking you if you want to create it. Click Yes.

  6. On page 4, the wizard tells you that it is ready to perform the upgrade. Click Next to generate the upgraded project.

Status text and a progress bar show you what the Upgrade Wizard is processing and give you a visual indicator of the time left. The upgrade is not instantaneous; it can take anywhere from a minute (for a small project such as ours) to several hours (for a project with hundreds of forms and classes). After the wizard has finished, your project opens in Visual Basic .NET. The result will be similar to Figure 5-5, depending on what windows you already have open in Visual Basic .NET.

Figure 5-5

The upgraded project opens in Visual Basic .NET.

What Just Happened?

Let s look at the upgraded project to see what the wizard did with our Visual Basic 6 project. You should see the Solution Explorer window in the top right corner of the IDE. (If the window is not visible, press Ctrl+Alt+L to see it.) Double-click Form1.vb to open the upgraded form. As you can see in Figure 5-6, the project looks exactly as it did in Visual Basic 6, with the exception that the Timer is now in the component tray beneath the form. Why has it moved? Nonvisual controls such as Timers, ToolTips, and Menus are now displayed in the component tray instead of on the form itself.

Figure 5-6

Upgraded form.

Now let s try running the project. Press F5, just as you would in Visual Basic 6. The first time you run an upgraded project in Visual Basic .NET, you are prompted to save the solution file. A solution file is similar to a group file in Visual Basic 6. A solution contains one or more projects, just as a group can contain one or more projects. In Visual Basic .NET, since every project must be part of a solution, one is created for you. Press Enter to save it with the default name. The project then runs, and Form1 begins showing a new random number every second, as shown in Figure 5-7.

Figure 5-7

Generating random numbers in the upgraded project.

Click Command1 to stop the Timer. The Timer indeed stops, but not in the way we intended. Instead, the program breaks with an exception: 0 is not a valid value for Interval. Interval must be greater than 0, as shown in Figure 5-8.

Figure 5-8

Exception generated by the upgraded project.

Click Continue to stop the program and return to the IDE. The exception occurs at this line in the Command1_Click event:

Me.Timer1.Interval = 0

What happened? Let s look at the upgraded code for the Command1_Click event:

Private Sub Command1_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Command1.Click    'UPGRADE_WARNING: Timer property Timer1.Interval cannot have a value  'of 0. Click for more:  'ms-help://MS.VSCC/commoner/redir/redirect.htm?key- 'word="vbup2020.htm" Me.Timer1.Interval = 0 End Sub

The Upgrade Wizard has inserted a warning: Timer Property Timer1.Interval cannot have a value of 0. Warnings alert you to run-time differences between Visual Basic 6 and Visual Basic .NET. Click the underlined part of the comment, and a Help topic opens in the IDE with the title Timer Interval Property Behavior Has Changed. Figure 5-9 shows what the Help topic looks like.

Figure 5-9

Help topic for the Timer Interval property.

The Help topic says that Timer.Interval no longer disables the timer. Instead you have to use the statement Timer1.Enabled = False. Let s change our code. Replace the line

Me.Timer1.Interval = 0

with

Me.Timer1.Enabled = False

Now press F5 to run the project. Notice that this time Command1 stops the timer just as it did in Visual Basic 6.

We ve finished upgrading our project, but let s not stop there. We ll do something with our upgraded project that you couldn t do in Visual Basic 6 make the form fade out. Add the following code to the Command1_Click event:

Dim i As Integer For i = 99 To 0 Step -1    Me.Opacity = i / 100 Next End

After you add this code, the event should look like this:

Private Sub Command1_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Command1.Click    'UPGRADE_WARNING: Timer property Timer1.Interval cannot have a value    'of 0. Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup2020.htm    Me.Timer1.Enabled = False    Dim i As Integer    For i = 99 To 0 Step -1       Me.Opacity = i / 100    Next    End End Sub

Press F5 to run the project. Now when you click Command1, the form becomes transparent and fades from view. As you can see, you may have to make some modifications to get your project working, but after you ve done so, you can take advantage of the many features in Visual Basic .NET that weren t available in Visual Basic 6. Upgrading enables you to add more value to your projects.

Language Changes

Now that we ve got our project working, let s take a few moments to review where the Upgrade Wizard changed the code, stepping through the original code line by line. Here is the original Visual Basic 6 code:

Option Explicit Private Sub Command1_Click()    Me.Timer1.Interval = 0 End Sub Private Sub Timer1_Timer()    Dim i As Integer    i = Rnd * 10    Me.Text1 = i End Sub

The first line, Option Explicit, is upgraded to

Option Strict Off Option Explicit On

Notice that On was added to Option Explicit. This statement has the same effect as the plain Option Explicit in Visual Basic 6 it means that all variables must be explicitly declared using the Dim, Public, or Private statement. As you might guess, you can also write Option Explicit Off, which lets you use variables without explicitly declaring them. Option Strict is a new option. When it is set to On, you will get compile errors if your code does either of the following:

  • Uses late binding

  • Performs a narrowing conversion that is, converts a variable of one type to another, where an overflow or type mismatch might occur (like assigning a Date to a String or an Integer to a Long)

When your project is upgraded, Option Strict Off is put at the top of each file to turn off strict type checking, since setting it to On would cause compile errors in most Visual Basic 6 code.

The Different Option Statements

Visual Basic .NET has three types of Option statements. Along with Option Strict and Option Explicit, there is Option Compare. As it did in Visual Basic 6, Option Compare sets the string comparison mode. By default, Strict is set to Off, Explicit is set to On, and Compare is set to Binary. You can change the defaults for each option in the Build section of the project s properties. If you change the default for, say, Option Compare to Text, all forms, modules, and classes will use Option Compare Text, unless they have their own Option Compare statements that override the default setting.

The wizard also inserts a Class statement. In Visual Basic .NET, all source files have a .vb file extension. Whether a file is a module, a class, or a form depends on the code inside the file. Why does the wizard insert a Class statement instead of a Form statement? Because all forms are actually classes that inherit from the base class System.Windows.Forms.Form. Here is what the Class statement looks like. (Event Code) indicates where your events are placed.

Friend Class Form1  Inherits System.Windows.Forms.Form  (Event Code) End Class

If you want to, you can put several forms or classes into a single file, provided each has its own Class statement.

Just beneath the Class statement, you ll see two collapsed regions: Windows Form Designer Generated Code and Upgrade Support, as shown in Figure 5-10.

Figure 5-10

Collapsed code.

This is system-generated code, or code used by Windows Forms to store the design layout of the form and by the Upgrade Wizard to store instancing information. We ll discuss these sections in Chapter 6 and in Chapter 12For now, let s move on and look at your upgraded event code. The Command1_Click event code is upgraded from

Private Sub Command1_Click()    Me.Timer1.Interval = 0 End Sub

to

Private Sub Command1_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Command1.Click    'UPGRADE_WARNING: Timer property Timer1.Interval cannot have a value     'of 0. Click for more: ms-help://MS.VSCC/commoner/redir/    'redirect.htm?keyword="vbup2020.htm"    Me.Timer1.Interval = 0 End Sub

The signature of the Click procedure has changed from Visual Basic 6. In Visual Basic .NET, most events are passed two parameters: eventSender, an object that is set to the control (in this case CommandButton1); and eventArgs, an object that contains extra arguments (such as mouse coordinates). The contents of eventArgs change depending on the type of event. For Click events, it contains nothing of interest. At the end of the Sub statement, you ll see the Handles keyword. This keyword associates the event procedure with the control event. Each event procedure can be associated with multiple controls and multiple events. For example, the following Handles clause would connect a procedure to the Command1.Click, Command2.Click, and Command1.Text Changed events:

Handles Command1.Click, Command2.Click, Command1.TextChanged

The line after the Sub statement contains the following upgrade warning:

'UPGRADE_WARNING: Timer property Timer1.Interval cannot have a value  'of 0. Click for more: ms-help://MS.VSCC/commoner/redir/ 'redirect.htm?keyword="vbup2020.htm"

The Upgrade Wizard inserts warnings whenever it encounters a statement that may not work the same in Visual Basic .NET. It inserts the warning on one line before the statement with the problem. You can filter the Task List to show the upgrade warnings. For information on using the Task List, see Chapter 6.

The final statement in the Command1_Click event is

Me.Timer1.Interval = 0

This statement is exactly the same as it was in Visual Basic 6.

Timer1_Tick has also undergone some changes during the upgrade. The Visual Basic 6 code

Private Sub Timer1_Timer()    Dim i As Integer    i = Rnd * 10    Me.Text1 = i End Sub

is upgraded to

Private Sub Timer1_Tick(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Timer1.Tick    Dim i As Short    i = Rnd() * 10    Me.Text1.Text = CStr(i) End Sub

Just as we saw with Command1_Click, the Timer1_Tick procedure now has a new signature. The line after the Sub statement has also changed. In Visual Basic .NET, the Integer data type has grown in size from 16 bits to 32 bits, and a new data type, Short, has been added to the language for 16-bit numbers. For this reason, the line

Dim i As Integer

is upgraded to

Dim i As Short

This ensures that the variable i is the same size as it was in Visual Basic 6. Let s move on to the next line:

i = Rnd * 10

is upgraded to

i = Rnd() * 10

In Visual Basic .NET, all calls to functions must have parentheses, so the Upgrade Wizard has added them to the Rnd function. Now let s look at the final line of code in this event procedure.

Me.Text1 = i

is upgraded to

Me.Text1.Text = CStr(i)

This line has undergone two changes. Because Visual Basic .NET does not support default properties, the Upgrade Wizard resolves the default property of Me.Text1 and expands the statement to Me.Text1.Text. Because the Text property is being assigned to a number, the wizard also explicitly converts i to a string, using the CStr function.

Whew! The upgrade resulted in quite a number of changes, but looking over the upgraded code you ll see that it still looks familiar. The overarching impression is that upgraded code is more explicit than the Visual Basic 6 version. That, in a nutshell, is the real difference between Visual Basic 6 and Visual Basic .NET. Visual Basic 6 concealed a lot of details, like forms being classes, the design layout, and default properties. In Visual Basic .NET, these are all exposed for you to see.

Other Files in Your Project

Now close the Code Editor and Windows Form Designer windows. Let s turn our attention to the Solution Explorer for a second. The Solution Explorer shows all the files in your upgraded project. Notice that the Upgrade Wizard has added two extra files to your project: _UpgradeReport.htm and AssemblyInfo.vb. Go ahead and double-click AssemblyInfo.vb to open it in the Code Editor. This file contains attributes. Attributes don t perform any action; they are used to store additional information about a method, form, class, or project. The attributes in this file allow you to change information about your project such as the assembly title and assembly description. Generally, unless you have a specific need, you can leave these values at their default settings. Close the AssemblyInfo.vb Code Editor and we ll look at the upgrade report.

A discussion of the assembly attributes in AssemblyInfo.vb is beyond the scope of this book. These attributes can be used to specify the identity of your Visual Basic .NET application. To learn more about attributes, search the Visual Basic .NET Help for Attributes and Setting Assembly Attributes.

The upgrade report is added to your project with the filename _UpgradeReport.htm. It is an HTML file. Double-click the file in the Solution Explorer to open it. Click the + sign next to Form1 to display all of the issues with Form1, and you ll see that the difference with Timer.Interval has been recorded. The report should look similar to Figure 5-11.

Figure 5-11

An upgrade report.

The upgrade report captures in one place the issues found while upgrading the project to Visual Basic .NET. Like the comments in code, each issue is also a hyperlink that navigates to a Help topic describing how to fix the problem. For more on upgrade issues, see Chapter 7.

You now have your Visual Basic .NET project. The Upgrade Wizard keeps the form layout the same, makes necessary syntax changes, and warns you about any issues found during the upgrade. After you get your project working, you can enhance it using some of the exciting new features of Visual Basic .NET. We ll put off a discussion of these features until later, because we still need to look at how to upgrade project groups and snippets of code.



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