Visual Basic 60 Language Elements

The upgrade wizard upgrades different Visual Basic 6.0 elements to Visual Basic .NET, as discussed in the next sections.

Declarations

The upgrade wizard automatically upgrades variable declarations to their equivalent.NET Framework types, following the Visual Basic .NET structure and taking into account the Visual Basic 6.0 declaration guidelines.

For example, in this Visual Basic 6.0 code, a and b are implicitly declared with the Variant data type, and c is declared explicitly as a 16-bit integer.

In Visual Basic .NET, variables must be declared explicitly, and there is no Variant data type. The upgrade wizard changes the code to declare a and b using the object data type, which is the .NET Framework's closest equivalent to Variant, and declares c as short, which is what Visual Basic .NET calls its 16-bit integer data type.

Dim a, b As Object Dim c As Short

Routines

Parameters can be passed to a procedure or function in one of two ways: by reference (ByRef) or by value (ByVal). When a parameter is passed to a subprocedure by reference, changes made to the parameter's value affect the actual variable or object passed by the calling procedure. When a parameter is passed by value, the subprocedure uses a copy of the original variable or object, and any changes the subprocedure makes to it do not affect the original.

By default, Visual Basic 6.0 uses the ByRef method when the parameter's data type is an intrinsic one such as Integer, Long, Boolean, String, and so on. When a parameter uses a non-intrinsic data type, Visual Basic 6.0 passes it by value by default.

By comparison, Visual Basic .NET passes all parameters by value unless otherwise specified. If a parameter keyword is specified in the signature a Visual Basic 6.0 routine, the upgrade wizard will generate the Visual Basic .NET routine accordingly. If ByVal or ByRef are not specified for a parameter in a Visual Basic 6.0 routine, the upgrade wizard will specify ByRef for the parameter.

The following Visual Basic 6.0 code uses both explicit and implicit methods to pass parameters.

Public Sub ByValTestSub(ByVal p1 As String)

p1 = "Hello World" End Sub

Public Function TestFunc(p1 As Integer, p2 As Integer) As Integer

TestFunc = p1 + p2 End Function

The upgrade wizard explicitly passes the parameters by reference to the TestFunc function, so it will operate the same way in Visual Basic .NET as it did in Visual Basic 6.0.

Public Sub ByValTestSub(ByVal p1 As String)

p1 = "Hello World" End Sub

Public Function TestFunc(ByRef p1 As Short, ByRef p2 As Short) As Short

TestFunc = p1 + p2 End Function

When a parameter is declared as Optional in Visual Basic 6.0, you do not have to specify a default value for the parameter. For example, this Visual Basic 6.0 subroutine can take an optional String parameter, p1.

Public Sub OpTestSub(Optional p1 As String) End Sub

In Visual Basic .NET, you must specify a default value when declaring an optional parameter. The subroutine uses the default value for the variable when the calling procedure does not supply the parameter. The upgrade wizard automatically adds default values for all optional parameters that do not already have them.

Public Sub OpTestSub(Optional ByRef p1 As String = "") End Sub

For numeric data types, the upgrade wizard uses a default value of 0. For strings, it uses an empty string (""). For objects, it uses Nothing as a default value.

Properties

In Visual Basic 6.0, properties are defined using the Property Get, Property Let, and Property Set statements. Visual Basic .NET replaces these statements with a new property declaration syntax that uses Get and Set accessors to provide access to the property.

This Visual Basic 6.0 code uses Property Get, Property Let, and Property Set statements to expose a property named Text.

Private mText As String Private mObj As Object

'Text properties

Public Property Get Text() As String

Text = mText End Property

Public Property Let Text(ByVal Value As String)

mText = Value End Property

'Object properties

Public Property Get Obj() as Object

Obj = mObj End Property

Public Property Set Obj(ByVal Value As Object)

Set mObj = Value End Property

The upgrade wizard combines these statements into a single property declaration for each property. In this particular example, two new property declarations are created, as shown here.

Private mText As String Private mObj As Object

Public Property Text() As String Get

Return mText

End Get

Set(ByVal Value As String)

mText = Value End Set End Property

Public Property Obj() As Object Get

Obj = mObj End Get

Set(ByVal Value As Object)

mObj = Value End Set End Property

In Visual Basic 6.0, you can create a read-only property by omitting the Property Let and Property Set statements. For example, to make the Text and Obj properties in the preceding example read-only, you can remove the Property Set and Property Let code, as shown here.

Private mText As String Private mObj As Object Private mText_w As String Private mObj_w As Object

Public Property Get Text() As String

Text = mText End Property

Public Property Get Obj() As Object

Set Obj = mObj End Property

Public Property Set TextW(value As String)

mText_w = value End Property

Public Property Set ObjW(value As Object)

Set mObj_w = value End Property

In Visual Basic .NET, read-only properties are declared using the ReadOnly modifier. When you upgrade a Visual Basic 6.0 read-only property to Visual Basic .NET, the upgrade wizard will add the ReadOnly modifier to the property declaration. An analogous declaration is made for write-only properties, where the upgrade wizard uses a WriteOnly modifier. The resulting code for this example is shown here.

Private mText As String Private mObj As Object Private mText_w As String Private mObj_w As Object

Public ReadOnly Property Text() As String Get

Return mText End Get End Property

Public ReadOnly Property Obj() As Object Get

Obj = mObj End Get End Property

Public WriteOnly Property TextW() As String Set(ByVal Value As String)

mText_w = Value End Set End Property

Public WriteOnly Property ObjW() As Object Set(ByVal Value As Object)

mObj_w = Value End Set End Property

Event Handlers

Events in Visual Basic .NET are built on top of delegates, so they are handled very differently from events in Visual Basic 6.0.

The upgrade wizard automatically handles the details surrounding events by generating signatures for the methods according to the rules of the .NET Framework, and by adding the necessary Visual Basic keywords to comply with the Visual Basic .NET events infrastructure.

For example, consider a Visual Basic 6.0 class, Class1 that defines an OnChange event with a Text property as follows.

Private mText As String

Public Event OnChange(ByVal Text As String)

Private Sub Change(ByVal Text As String)

RaiseEvent OnChange(Text) End Sub

Public Property Get Text() As String

Text = mText End Property

Public Property Let Text(ByVal Value As String) mText = Value Call Change(Value) End Property

A Visual Basic 6.0 client application consumes the event. The application contains a TextBox control and a Button control, and creates a Class1 object called Instance. The application contains a form load event (Form_Load), a button click event (Command1_Click), and a Class1 change event (Instance_OnChange). When the button is clicked, the text property of the Class1 is modified, that in turn raises the Instance_OnChange event.

Public WithEvents Instance As Class1

Private Sub Command1_Click()

Instance.Text = Text1.Text End Sub

Private Sub Form_Load()

Set Instance = New Class1 End Sub

Private Sub Instance_OnChange(ByVal Text As String)

Call MsgBox("Changed to: " + Text) End Sub

When Class1 is upgraded to Visual Basic .NET with the upgrade wizard, it produces the following code.

Option Strict Off Option Explicit On Friend Class Class1

Private mText As String

Public Event OnChange(ByVal Text As String)

Private Sub Change(ByVal Text As String)

RaiseEvent OnChange(Text) End Sub

Public Property Text() As String Get

Return mText End Get

Set(ByVal Value As String) mText = Value Call Change(Value) End Set End Property End Class

The upgrade wizard upgrades the client as follows.

Option Strict Off Option Explicit On Friend Class Forml

Inherits System.Windows.Forms.Form #Region "Windows Form Designer generated code "

Public WithEvents Textl As System.Windows.Forms.TextBox Public WithEvents Commandl As System.Windows.Forms.Button

#End Region

#Region "Upgrade Support " #End Region

Public WithEvents Instance As Classl

Private Sub Command1_C1ick(ByVa1 eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Commandl.Click Instance.Text = Textl.Text End Sub

Private Sub Forml_Load(ByVa1 eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles MyBase.Load Instance = New Classl End Sub

' UPGRADE_NOTE: Text was upgraded to Text_Renamed. ... Private Sub Instance_OnChange(ByVa1 Text_Renamed As String) Handles Instance.OnChange

Call MsgBox("Changed to: " & Text_Renamed) End Sub End Class

In both cases, the upgrade wizard has upgraded the Visual Basic 6.0 code to Visual Basic .NET, and no additional modifications are required.

An important change related to events is in parameter passing. In Visual Basic 6.0, the event subroutine contains the name and type of each parameter. In Visual Basic .NET, the parameters are bundled up in an EventArgs object and a reference to this object is passed to the event handling subroutine. Also, the event subroutine will receive a reference to the object that initiated the event.

To illustrate the difference in parameter passing for event handling subroutines, consider a Visual Basic 6.0 form that contains a ListBox control named List1. If List1 is set up with the check box style, you can have an event handler deal with a checked item. This is shown in the following code example.

Private Sub Listl_ItemCheck(Item As Integer)

MsgBox "You checked item: " & Item End Sub

The upgrade wizard can be applied to the code. This results in an upgraded version of the code with the new event handler format. The following code example shows the results.

' UPGRADE_WARNING: ListBox event List1.ItemCheck has a new behavior. Private Sub List1_ItemCheck(ByVal eventSender As System.Object, _

ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) _ Handles List1.ItemCheck MsgBox("You checked item " & eventArgs.Index) End Sub

Notice how the item that is checked is passed directly as a parameter in the original code. In contrast, it is passed as a member of the ItemCheckEventArgs object eventArgs in the upgraded version. Note, too, that the original version required only the selected item as a parameter, where the upgraded version requires the event arguments object, as well as a reference to the object that initiated the event.

Control Structures

Some of the Visual Basic .NET control structures are slightly different from the control structures in Visual Basic 6.0. For example, the Wend keyword, that ends While loops in Visual Basic 6.0, is replaced by End While in Visual Basic .NET. The upgrade wizard automatically upgrades Visual Basic 6.0 flow statements and preserves their functionality.

The following Visual Basic 6.0 code demonstrates the If.. .Then, For.. .Next, and While...Wend control structures.

Dim i As Integer Dim bol As Boolean Dim value As Integer bol = True

Wend

The upgrade wizard upgrades the control structures to Visual Basic .NET as follows.

Dim i As Short Dim bol As Boolean Dim value As Short bol = True

Modules and Classes

Modules and classes are fully supported by the upgrade wizard. In Visual Studio 6.0, module and class files have distinct file name extensions (.bas for modules, .cls for classes), and are organized in specific folders within the Project folder.

In Visual Basic .NET, modules and class files both have the same file name extension, .vb, and are distinguished from one another by the structure within each file.

The upgrade wizard automatically handles file name upgrades. For each element, the upgrade wizard will compose a name based on a specific structure. The following code example shows how the upgrade wizard handles a name upgrade for a module.

Module MODULENAME End Module

The following is for classes.

Friend Class Class1 End Class

Interfaces

In Visual Basic 6.0, common classes can be treated as interfaces. Consider the following Visual Basic 6.0 class, Class1.

Class1:

Public Sub Test()

MsgBox "Hello from class 1" End Sub

Class2 uses the implements keyword to implement Classl.

Class2:

Implements Class1 Public Sub Class1_Test()

MsgBox "Hello from class 2" End Sub

Public Sub Test() Dim c As Class1 Set c = New Class1 c.Test End Sub

When the upgrade wizard upgrades Classl to Visual Basic .NET, it also creates an interface with a similar name as shown here.

Interface _Class1

Sub Test() End Interface Friend Class Class1 Implements _Class1

Public Sub Test() Implements _Class1.Test

MsgBox("Hello from class 1") End Sub End Class

The upgrade wizard generates both a class and an interface to replicate the behavior of Visual Basic 6.0. In this case, an object can be used both as an interface and as a regular class. Class2 that implements Classl and uses it as a regular class is upgraded as follows.

Friend Class Class2

Implements _Class1

Public Sub Class1_Test() Implements _Class1.Test

MsgBox("Hello from class 2") End Sub

Public Sub Test()

Dim c As _Class1 c = New Class1 c.Test() End Sub End Class

Web Classes

In Visual Basic 6.0, WebClass projects (also known as IIS application projects) are used to create Web applications based on Active Server Pages (ASP) technology. In Visual Basic .NET, ASP.NET Web application projects are used to create Web applications based on the newer ASP.NET technology.

Visual Basic 6.0 WebClass projects can be upgraded to ASP.NET Web Application projects using the upgrade wizard. The process for upgrading WebClass projects is essentially the same as for any other project type; however, there are some related issues that you should consider.

There are two prerequisites for upgrading a WebClass project:

• Internet Information Services (IIS) must be installed and running on the upgrade computer.

• You must have administrative privileges that allow you to install applications.

When upgrading WebClass projects with the upgrade wizard, the following scenarios take place:

• When a WebClass project is upgraded, the upgrade wizard will use a new project name by default, projectname.NET, where projectname is the name of the Visual Basic 6.0 project. This name is then used when naming the virtual directory. This virtual directory will also be configured as an application in IIS; and the application-friendly name will be the project name.

• If the virtual directory name already exists on the http://localhost server (which is typical if you repeat the application of the upgrade wizard on the same project in order to resolve upgrade issues), the upgrade wizard will append a number to the names of the virtual directory and the project to ensure uniqueness.

• When a Visual Basic 6.0 WebClass project is upgraded to Visual Basic.NET, the .asp file for the project is upgraded to an .aspx file. However, any references to the .asp file within an HTML template file are not automatically changed to .aspx references. This is because a template file might contain references to other .asp files that were not part of the WebClass project.

• The upgrade wizard only copies HTML template files to the new project directory. Any other .html files or image files are not copied to the new directory.

• When the upgrade wizard adds HTML files to a Visual Basic .NET project, it adds them as content files by default. When a WebClass project is upgraded, HTML files are added as embedded resources. If you add HTML files to the project after it is upgraded, you must set their Build Action property to Embedded Resource to make them visible to the application.

• The scope of Function and Sub procedures in your Visual Basic 6.0 code (for example, ProcessTags or Respond) will be changed from Private to Public to allow the WebClass Compatibility runtime to execute them.

• Declarations will be added to your project for the WebClass and for each of the Webltems and Templates in the WebClass project.

• A Page_Load event procedure will be added to the project, creating a WebClass object as well as WebItem object for each of the WebItems and Templates associated with the Visual Basic 6.0 WebClass project.

• In the Page_Load event procedure, you will see a call to the WebClass Compatibility runtime, WebClass.ProcessEvents. This allows the runtime to render the Webltem specified in the request URL. This code is the only new code added to your upgraded project, and is only added to emulate the underlying behavior of the Visual Basic 6.0 WebClass runtime.

Note: There are a number of behavioral differences for properties, methods, and events between ASP and ASPNET objects. For more information on behavioral differences, see Chapter 4, "Common Application Types," and Appendix C, "Introduction to Upgrading ASP"

Late Binding

Both Visual Basic 6.0 and Visual Basic .NET support late-bound objects. This is the practice of declaring a variable as the Object data type and assigning it to an instance of a class at run time. However, during the upgrade process, late-bound objects can introduce problems when default properties are resolved, or in cases where the underlying object model has changed and properties, methods, and events need to be upgraded.

For example, consider a Visual Basic 6.0 form called Form1 with a label called Label1. The following Visual Basic 6.0 code example sets the caption of the label to "SomeText".

Dim o As Object Set o = Me.Label1 o.Caption = "SomeText"

In Visual Basic .NET, which uses the .NET Framework's Windows Forms classes, the Caption property of a label control is now called Text. When the upgrade wizard upgrades your code to Visual Basic .NET, it upgrades the Caption property to Text on all of your Label objects. However, because late-bound objects are type-less, the upgrade wizard cannot detect how you intend to use them, or if any properties should be translated. In such cases, you will need to change the code yourself after it is upgraded.

There are two ways to correctly upgrade late-bound objects:

• You can rewrite the original Visual Basic 6.0 code using early-bound objects, so that the upgrade wizard will upgrade it automatically, as shown here.

Dim o As Label Set o = Me.Label1 o.Caption = "SomeText"

As a rule, you should declare variables using the appropriate object type instead of the general Object data type whenever possible.

• You can modify the Visual Basic .NET code obtained after the upgrade process to eliminate unnecessary late-bound variables and manually upgrade the affected class members, as shown here.

Dim o As System.Windows.Forms.Label Set o = Me.Label1 o.Text = "SomeText"

Note: You can use the ArtinSoft Visual Basic Upgrade Wizard Companion as an alternative solution to the late-bound variable problem. The ArtinSoft Visual Basic Upgrade Wizard Companion Type Inference feature deduces the most appropriate data types for variables, parameters, and returns values, and avoids using generic data types such as Object. When the companion tool encounters an Object variable, it declares the variable using the appropriate type so that you do not have to make manual modifications. For more information about the ArtinSoft Visual Basic Upgrade Wizard Companion, go to the ArtinSoft Web site.

Type Casts

The upgrade wizard upgrades type casting statements using the appropriate type restrictions. For example, this Visual Basic 6.0 code converts a 16-bit integer to a string, and then back to an integer.

Dim i As Integer Dim s As String i = 1

The Visual Basic 6.0 Integer data type corresponds to the Visual Basic .NET Short data type. The upgrade wizard uses the appropriate casting statement when it creates the Visual Basic .NET code, as shown here.

Enums

Enumerations, or enums, are a special type of value type. An enum has a name and a set of fields that define values for a primitive data type.

The upgrade wizard automatically upgrades enumerated types and their references to the format used by Visual Basic .NET. For example, the following Visual Basic .NET code declares and uses an enumerated type.

Public Enum MyEnum FirstValue SecondValue ThirdValue End Enum

Public Sub TestEnum() Dim e As MyEnum e = SecondValue End Sub

The upgrade wizard automatically modifies the code for Visual Basic .NET as follows.

Public Enum MyEnum FirstValue SecondValue ThirdValue End Enum

Public Sub TestEnum() Dim e As MyEnum e = MyEnum.SecondValue End Sub

User-Defined Types

In Visual Basic 6.0, you can use user-defined types (UDTs) to create groups of data items of different types. In Visual Basic .NET, UDTs are called structures. A structure associates one or more members with each other and with the structure itself. When you declare a structure, it becomes a composite data type, and you can declare variables of that type.

UDTs and the Upgrade Wizard

The upgrade wizard automatically upgrades UDTs to Visual Basic .NET structures. For example, the following Visual Basic 6.0 code defines and uses a UDT.

Private Type MyType x As Integer y As Integer name As String End Type

Public Sub TestUDT() Dim udt As MyType udt.name = "Joe" udt.x = 5 udt.y = 10 End Sub

The Visual Basic .NET code produced by the upgrade wizard uses the Structure keyword to redefine the UDT as a structure.

Private Structure MyType Dim x As Short Dim y As Short Dim name As String End Structure Public Sub TestUDT() Dim udt As MyType udt.name = "Joe" udt.x = 5 udt.y = 10 End Sub

Upgrading UDTs with Fixed-Length Fields

The previous example shows a UDT that contains native data type fields. The size of these fields is determined by the programming language and the way it is compiled. The fact that there are equivalent .NET data types for each of the original fields allows the automatic upgrade to be performed; this is not always the case.

It is typical to have UDTs that contain fixed-length fields that are composed of basic data elements, such as arrays or fixed-length strings. This type of UDT is commonly used when accessing legacy flat files, as discussed in Chapter 11, "Upgrading String and File Operations," in the section, "Accessing Fixed-Length Records Using UDTs."

The following example shows a Visual Basic 6.0 UDT that contains fixed-length fields.

Type employee name As String * l5 1ast_name As String * 20 department As String * 6 phone_ext(3) As Long salary As Integer End Type

The upgrade wizard upgrades the code as follows.

Structure employee

<VBFixedString(l5),System.Runtime.InteropServices.Marsha1As( _ System.Runtime.InteropServices.UnmanagedType.ByValTStr, _ SizeConst:=l5)> Public name As String

<VBFixedString(20),System.Runtime.InteropServices.Marsha1As( _ System.Runtime.InteropServices.UnmanagedType.ByValTStr, _ SizeConst:=20)> Public 1ast_name As String

<VBFixedString(6),System.Runtime.InteropServices.Marsha1As( _ System.Runtime.InteropServices.UnmanagedType.ByValTStr, _ SizeConst:=6)> Public department As String

<VBFixedArray(3)> Dim phone_ext() As Integer Dim salary As Short

' UPGRADE_TODO: "Initialize" must be called to initialize instances of ' this structure. Click for more:

' 'ms-he1p://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbupl026"' Public Sub Initia1ize()

ReDim phone_ext(3) End Sub End Structure

The fields whose data type is a fixed-length string cannot be directly declared in the Visual Basic .NET structure because fixed-length strings are not a native type in

Visual Basic .NET and cannot be included in structures. One possible solution that can be used in variable declarations is to use the VB6.FixedLengthString compatibility class, but because it is an object it also cannot be included in structures.

To solve the problem for structures, a programmer can indicate that a field in a structure must be treated as a fixed-length element for file I/O purposes. This functionality corresponds to the .NET marshaling capabilities and is specified using the VBFixedString or VBFixedArray attributes; the argument passed to these attributes represents the size of the field. In this manner, an equivalent Visual Basic .NET structure can be obtained. The preceding Visual Basic .NET code example demonstrates the use of this marshaling attribute.

Note: The technique shown here is applicable only when dealing with fixed-length strings and arrays in file I/O scenarios. In interop situations, you will have to use the MarshalAs attribute to properly deal with fixed-length fields. For more information about the MarshalAs attribute, see Chapter 14, "Interop Between Visual Basic 6.0 and Visual Basic .NET."

0 0

Post a comment