In the previous chapter, we looked at the VB .NET IDE in some detail. We expand on what we learned in the previous chapter by detailing some of the differences between VB6 and Visual Basic .NET. The Windows XP Tablet PC Edition Platform Software Development Kit (SDK) can be used with VB6 or VB .NET, although this book uses .NET. This chapter helps VB6 users to get up to speed with VB .NET. First, we look at how you can use the Upgrade Wizard to move your products from VB6 to VB .NET, and then we look at the changes that have been made to the programming language.
The Visual Basic .NET and Visual Basic 6.0 IDEs can be used on the same computer and can even execute simultaneously. This is something very new as previous versions of Visual Basic caused considerable problems for one another if they were installed on the same machine. Additionally, applications written and compiled in Visual Basic .NET and Visual Basic 6.0 can be installed and executed on the same computer. Although the projects in this book have been written for Visual Basic .NET, they can be written so that they will work in VB6.
Because of the new changes associated with Visual Basic .NET, VB6 code needs to be upgraded before it can be used. Fortunately, the vast majority of the time, this is very easy because it happens automatically when you open a Visual Basic 6 project in Visual Basic .NET. This is not a perfect upgrade, however, and you'll often be left with a list of tasks that the wizard could not handle on its own.
An Upgrade Wizard, which can be seen in Figure 3.1, steps you through the upgrade process and creates a new Visual Basic .NET project. The existing Visual Basic 6 project is left unchanged. If you have Visual Basic version 5 projects, it's best to upgrade them to 6 before moving on to .NET (or version 7 as it is sometimes called).
Figure 3.1: The Upgrade Wizard makes it easy to convert version 6 projects to Visual Basic .NET.
When your project is upgraded, the language is modified for any syntax changes and your Visual Basic 6.0 forms are converted to Windows Forms. Depending on your application, you may need to make minor changes to your code after it is upgraded. Many times, this can be necessary because certain features either are not available in Visual Basic .NET or the features have changed significantly enough to warrant manual changes.
After your project is upgraded, Visual Basic .NET provides an 'upgrade report' to help you make changes and review the status of your project. The items are displayed as tasks in the new Task List window, so you can easily see what changes are required, and navigate to the code statement simply by double-clicking the task. Many times, the document recommendations simply represent good programming practices, but they also identify the Visual Basic 6 objects and methods that are no longer supported.
When your VB6 code is upgraded to Visual Basic .NET, it follows a specific set of rules. The following rules list some basic information that you should keep in mind if you are currently planning to upgrade a VB6 project to .NET.
Previous versions of Visual Basic supported the Variant data type, which could be assigned to any primitive type. In fact, it is the default data type if a variable wasn't declared in VB6. VB .NET converts variants to the Object data type.
In Visual Basic .NET, the data type for 16-bit whole numbers is now Short. The data type for 32-bit whole numbers is now Integer, and Long is now 64 bits.
Table 3.1 shows a few examples.
VB6 |
VB .NET |
---|---|
Dim A as Integer |
Dim A as Short |
Dim B as Long |
Dim B as Integer |
N/A |
Dim A as Long |
Variant |
N/A (Use new ‘Object') |
Currency |
N/A (Use Decimal or Long) |
N/A |
Decimal |
String |
String (doesn't support fixed-length strings) |
The vast majority of API calls expect 32-bit values if they take numeric arguments. With the previous section in mind, you can see that problems are sure to arise. For instance, in VB6, a 32-bit value is a Long data type, whereas in .NET, a Long is 64 bits. You'll have to use Integer as the data type in .NET to make the calls correctly. According to Microsoft documentation, many APIs will no longer be callable from VB or may have replacements.
The Upgrade Wizard tries to correct API calls by creating wrappers for them. This is not a good idea and you should look at every API call individually to make any changes you need.
The following code samples are an example of an API call under VB6 and under VB .NET.
Listing 3.1 Visual Basic 6.
Private Declare Function GetVersion Lib "kernel32" () As Long Function GetVer() Dim Ver As Long Ver = GetVersion() MsgBox ("System Version is " & Ver) End Function
Listing 3.2 Visual Basic .NET.
Private Declare Function GetVersion Lib "kernel32" () As Integer Function GetVer() Dim Ver As Integer Ver = GetVersion() MsgBox("System Version is " & Ver) End Function
VB .NET introduces several new keywords that have no counterpart in VB6. Table 3.2 details some of them:
Keyword |
Notes |
---|---|
Catch |
New error handling; indicates code to use to process errors |
Char |
New character data type |
Finally |
New error handling; indicates code to use to run regardless of errors |
Imports |
Makes an object hierarchy (namespace) available in a module |
Inherits |
Points to a base class for inheritance |
MustOverride |
Indicates that any class that derives from this class must supply an override for this member |
MyBase |
References the base class for use by subclass code |
Namespace |
Specifies a namespace for a module |
Overloads |
Indicates there's more than one version of a function and the compiler can distinguish among them by the input parameters |
Overrides |
Indicates a member overrides the identically named member in the base class |
Overridable |
Allows a member to be overridden in any class derived from the base class |
Protected |
Is only available to classes derived from this class |
ReadOnly |
Is used in a property that contains only a 'Get' |
Shared |
Indicates all instances of a class should share a variable in a class |
Throw |
New error handling; allows you to raise an error |
Try |
New error handling; starts code with error handling enabled |
Webmethod |
Tags a method as part of a publicly available Web service |
WriteOnly |
Is used in a property that contains only a 'Set' |
Like the additions from the previous section, there are also some items that have been removed in VB .NET. The following list details some of the removed keywords and statements:
Table 3.3 details some commands that have equivalents in VB .NET:
VB6 |
VB .NET Namespace |
Method/Property |
---|---|---|
Circle |
System.Drawing.Graphics |
DrawEllipse |
Line |
System.Drawing.Graphics |
DrawLine |
Atn |
System.Math |
Atan |
Sgn |
System.Math |
Sign |
Sqr |
System.Math |
Sqrt |
Lset |
System.String |
PadRight |
Rset |
System.String |
PadLeft |
Rnd |
Microsoft.VisualBasic.Compatibility.VB6 |
Rnd |
Round |
Microsoft.VisualBasic.Compatibility.VB6 |
Round |
DoEvents |
System.Winform.Application |
DoEvents |
This is certainly not an exhaustive list as there are sure to be other items that have been added and removed. However, this list should serve as a good focal point for your work. There are a couple of additional commands that are no longer supported. One in particular is the Set command, which has been removed so that the following code in VB 6
Set objObject = objAnotherObject
will be changed to:
objObject = objAnotherObject
Another widely used command that has been changed is the Debug command, which was used as follows in VB6:
Debug.Print
In VB .NET, this is now:
Debug.Write Debug.WriteLine
The use of arrays in VB .NET has also changed. In Visual Basic 6, if you declare an array like the following, you would get 11 items from 0 to 10:
Dim number(10) as Integer
Within VB .NET, the same array only gives you 10 items from 0 to 9.
In Visual Basic 6, a control or object had a default property that wouldn't need to be specified. For instance, if you wanted to set a TextBox equal to a string, you would simply use:
txtInformation = "This is a string"
Visual Basic .NET doesn't support default properties. So instead, you have to make sure to specify it as follows:
txtInformation.Text = "This is a string"
Controls in Visual Basic 6 were public. That is, you could simply reference a control on form 1 inside the Code Editor on form 2 by simply using form1.textbox1.text. In VB .NET, you'll have to create a public Let and Get property procedure for every control property you want to have access to.
Get and Let are now combined in VB .NET, so instead of being two separate property procedures, you create one.
Listing 3.3 Visual Basic 6 code.
Property Get PropertyA() As Integer m_PropertyA = PropertyA End Property Property Let PropertyA(NewValue As Integer) m_PropertyA = NewValue End Property
Listing 3.4 Visual Basic .NET code.
Property PropertyA() As Short Get m_PropertyA = MyPropertyA End Get Set m_PropertyA = Value End Set End Property
Visual Basic .NET forms are now called Windows Forms. The following list gives you an idea of the differences between version 6 and .NET forms:
In this chapter, we looked at some of the changes that have been made to VB .NET. There are complete books written on this subject, so this chapter should not be considered an all-inclusive list of changes. However, it should serve its purpose of providing you with the ideas that have been altered and will help you in subsequent chapters. In Chapter 4, Basics of the .NET Framework, we spend a little time looking at the Visual Basic .NET Framework. After we have a little more background information, we move on to writing several complete applications in VB .NET.