- Keyword Changes
- Functional Changes
- Upgrading from VB 6.0
- Summary
Upgrading from VB 6.0
Although it is recommended that you not use the compatibility classes directly, the VB 6.0 Upgrade Wizard might use them when upgrading projects from VB 6.0 to VB .NET. Before upgrading a previous project to VB .NET, you must decide whether you should.
Basically, this question must be answered on a project-by-project basis, but for most projects the answer will be no. The reasons for this are as follows:
-
As evidenced by this chapter, many changes have been made to the structure of the language, and although the Upgrade Wizard makes some automatic corrections for these, it likely will not address all of them. In fact, Microsoft recommends several coding practices that should be employed in existing VB 6.0 projects for the upgrade to proceed more smoothly (http://msdn.microsoft.com/library/default.asp?URL=/library/techart/vb6tovbdotnet.htm). Because not all VB 6.0 projects follow these guidelines, there will be plenty of places where you need to change the code. This also will entail extensive testing, which, in the final analysis, might be more effort than is warranted given the reasons listed here.
VB .NET and the Services Framework present totally new ways of dealing with certain scenarios. For example, ADO.NET exposes a new disconnected programming model for database applications that is different from the classic ADO model. When you upgrade a VB 6.0 project, the Upgrade Wizard does not convert your code to ADO.NET, rather it uses a translation layer (COM Interop, discussed in Chapter 9, "Accessing Component Services") to broker calls between managed code and classic COM components such as ADO. In addition, new project types such as the Windows Service application allow you to write applications in a different way. An upgraded NT service project that relies on a third-party ActiveX control or the Win32 API will not take advantage of the inherent integration. As a result, you won't get the benefits of the Services Framework by upgrading your VB 6.0 project.
As a follow-on to the previous point, the Web Services model, perhaps the most important concept in .NET (discussed in Chapter 11, "Building Web Services"), will not automatically be integrated into your project. The design and implementation of Web Services likely will require redesign and implementation of your solutions.
As discussed in Chapters 4 and 6 particularly, VB .NET contains entirely new language constructs, such as implementation inheritance and structured exception handling. The Upgrade Wizard will not add these powerful features to your code. That is a task for subsequent development.
Although the .NET platform is a new and interesting way to develop applications, existing VB 6.0 applications will be supported and will run just fine on Windows operating systems. In fact, VB 6.0 and VS .NET can coexist peacefully on the same machine.
As a corollary to the previous point, if your applications follow a multitiered architecture where the data access and business services are decoupled from the user interface, you can take a piecemeal approach and recode the user interface in ASP.NET to take advantage of its features while using COM Interop to call existing COM components that perform business logic and data access. In this way, the application needn't be reworked from the ground up, and other applications that rely on your middleware will continue to function.
In any case, the Upgrade Wizard cannot upgrade certain types of projects in VB 6.0 (and yes, the project must have been previously saved in VB 6.0). For example, DHTML applications, data binding with DAO and RDO, and ActiveX document projects will not be upgraded, whereas WebClasses will require significant modification.
So what is the recommendation? Probably only the simplest of applications, such as utility applications, are suited for a simple upgrade, compile, and run. Any project that interoperates with other COM components and external DLLs will require significant work that is tantamount to a rewrite. As a result, I would use the Upgrade Wizard simply as a starting point and more of a check on syntactical changes. For example, you might run the Upgrade Wizard on your data access COM component to see how the ADOMD library is referenced and accessed in VB .NET. However, then you need to do the work of integrating new language features and Services Framework classes as appropriate.
Using the Upgrade Wizard
To run the wizard, you simply need to open the VB 6.0 project files (.vbp) in VB .NET and follow the wizard steps. The wizard analyzes your code and performs certain automatic translationsfor example, from Long to Integer and Variant to Object. In addition, it places various warnings and errors as comments in the code. These are places you'll need to concentrate on. To summarize the upgrade, an HTML file is added to the project, which shows each error and provides additional information.
To illustrate the upgrade process, consider an ActiveX DLL project created in VB 6.0 called SearchSortUtils. This project contains a single class module called Algorithms that implements BubbleSort, ShellSort, and BinSearch (binary search) methods that work on zero-based multidimensional arrays. This project was run through the Upgrade Wizard, and Figure 3.1 shows the resulting upgrade report.
NOTE
The SearchSortUtils VB 6.0 project files can be downloaded from the companion Web site.
As you'll notice from the report, 0 errors and 57 warnings were issued in a project that comprised less than 275 lines in VB 6.0. The 57 warnings were primarily related to the fact that the wizard could not find a default property on variables that were of type Variant and converted to Object.
Figure 3.1 This HTML document shows that 57 issues were found when upgrading the SearchSortUtils project.
NOTE
The Upgrade Wizard can resolve parameterless default properties for classes that are referenced, but it cannot do so for objects that are late-bound, hence the warnings discussed previously.
Because this is a simple project that does not contain any references to COM components, ActiveX controls, or other external code, the project was immediately able to be compiled and run in VB .NET. However, to make the code more VB .NET friendly, and to illustrate the kinds of changes you'll make to your own code, the following changes were made:
The wizard changed references from Variant to Object. Because this class used Variant to refer to arrays, Object was changed to Array.
The methods were changed to shared methods so that a client does not have to create an instance of the class to use its methods.
Because VarType is obsolete, the references to the VarType function were changed to the GetType function and the GetType method of the variable being inspected.
The function return syntax was changed to use the Return statement rather than the name of the function.
Overloaded signatures were created for each of the methods rather than Optional arguments.
Listing 3.4 shows the completely upgraded project with the preceding changes.
Listing 3.4 Upgraded Utility Project. This listing shows the syntax of the upgraded SearchSortUtil project.
Option Strict Off Option Explicit On Public Class Algorithms ` Searching and sorting algorithms for zero-based ` variant two dimensional arrays where the first ` dimension is the columns and the second is the rows `****************************************************************** Public Overloads Shared Sub BubbleSort(ByRef varArray As Array) Call BubbleSort(varArray, False, 0) End Sub Public Overloads Shared Sub BubbleSort(ByRef varArray As Array, _ ByVal flDescending As Boolean) Call BubbleSort(varArray, flDescending, 0) End Sub Public Overloads Shared Sub BubbleSort(ByRef varArray As Array, _ ByVal flDescending As Boolean, ByVal pIndex As Short) Dim i As Integer Dim j As Integer Dim lngUB As Integer Dim lngLB As Integer Dim lngUB1 As Integer Dim lngLB1 As Integer Dim z As Integer Dim varArrTemp As Object ` Cache the bounds lngUB = UBound(varArray, 2) lngLB = LBound(varArray, 2) lngUB1 = UBound(varArray, 1) lngLB1 = LBound(varArray, 1) ` If the optional index is 0 then set it to the ` lower bound (sort by first column) If pIndex = 0 Then pIndex = LBound(varArray, 1) End If ` Loop through the array using the second ` dimension of the array (the rows) For i = lngLB To lngUB For j = lngLB To lngUB - 1 - i ` Compare the items in the array If CompMe(varArray(pIndex, j), _ varArray(pIndex, j + 1), flDescending) Then ReDim varArrTemp(UBound(varArray, 1), 0) ` If the first item is larger then swap them For z = lngLB1 To lngUB1 varArrTemp(z, 0) = varArray(z, j) Next z For z = lngLB1 To lngUB1 varArray(z, j) = varArray(z, j + 1) Next z For z = lngLB1 To lngUB1 varArray(z, j + 1) = varArrTemp(z, 0) Next z End If Next j Next i End Sub `****************************************************************** Public Overloads Shared Sub ShellSort(ByRef varArray As Array) Call ShellSort(varArray, False, 0) End Sub Public Overloads Shared Sub ShellSort(ByRef varArray As Array, _ ByVal flDescending As Boolean) Call ShellSort(varArray, flDescending, 0) End Sub Public Overloads Shared Sub ShellSort(ByRef varArray As Array, _ ByVal flDescending As Boolean, ByVal pIndex As Short) Dim i As Integer Dim lngPos As Integer Dim varTemp As Object Dim lngLB As Integer Dim lngUB As Integer Dim lngSkip As Integer Dim flDone As Boolean Dim z As Integer Dim varArrTemp As Object ` If the optional index is 0 then set it to the ` lower bound (sort by first column) If pIndex = 0 Then pIndex = LBound(varArray, 1) End If ` Cache the lower and upper bounds lngLB = LBound(varArray, 2) lngUB = UBound(varArray, 2) ` Assign the skip count Do lngSkip = (3 * lngSkip) + 1 Loop Until lngSkip > lngUB Do ` Decrement the skip each time through the loop lngSkip = lngSkip / 3 ` Check the remainder of the array For i = lngSkip + 1 To lngUB ` Pick up the current value varTemp = varArray(pIndex, i) ReDim varArrTemp(UBound(varArray, 1), 0) For z = LBound(varArray, 1) To UBound(varArray, 1) varArrTemp(z, 0) = varArray(z, i) Next z lngPos = i ` If we've reached the beginning then increment the ` skip count but signal that this is the last pass If lngSkip = 0 Then lngSkip = 1 flDone = True End If ` Check to see if the preceding element is larger Do While CompMe(varArray(pIndex, lngPos - lngSkip), _ varTemp, flDescending) ` If so then slide it in For z = LBound(varArray, 1) To UBound(varArray, 1) varArray(z, lngPos) = varArray(z, lngPos - lngSkip) Next z lngPos = lngPos - lngSkip If lngPos <= lngSkip Then Exit Do Loop ` Put the current value back down For z = 0 To UBound(varArray, 1) varArray(z, lngPos) = varArrTemp(z, 0) Next z Next i Loop Until lngSkip = lngLB Or flDone End Sub `****************************************************************** Private Shared Function CompMe(ByVal pArg1 As Object, _ ByVal pArg2 As Object, ByVal pDesc As Boolean, _ Optional ByRef pEqual As Boolean = False) As Boolean ` If descending then do a less than compare If pDesc Then ` If equality is specified then use an equal sign Select Case pEqual Case True ` Check if its a string to do a string compare If pArg1.GetType Is GetType(System.String) Then If UCase(pArg1) <= UCase(pArg2) Then Return True End If Else If pArg1 <= pArg2 Then Return True End If End If Case False ` If not specified then do a < compare If pArg1.GetType Is GetType(System.String) Then If StrComp(pArg1, pArg2, CompareMethod.Text) = -1 Then Return True End If Else If pArg1 < pArg2 Then Return True End If End If End Select Else ` If ascending doing a greater than compare Select Case pEqual Case True ` Check if its a string first If pArg1.GetType Is GetType(System.String) Then If UCase(pArg1) >= UCase(pArg2) Then Return True End If Else If pArg1 >= pArg2 Then Return True End If End If Case False ` Check if its a string If pArg1.GetType Is GetType(System.String) Then If StrComp(pArg1, pArg2, CompareMethod.Text) = 1 Then Return True End If Else If pArg1 > pArg2 Then Return True End If End If End Select End If Return False End Function `****************************************************************** Public Overloads Shared Function BinSearch(ByRef varArray As Array, _ ByVal varSearch As Object, ByVal pIndex As Short) As Integer Call BinSearch(varArray, varSearch, pIndex, False) End Function Public Overloads Shared Function BinSearch(ByRef varArray As Array, _ ByVal varSearch As Object, ByVal pIndex As Short, _ ByVal flPartial As Boolean) As Integer Dim lngLow As Integer Dim lngUpper As Integer Dim lngPos As Integer ` Set the upper and lower bounds of the array lngLow = LBound(varArray, 2) lngUpper = UBound(varArray, 2) Do While True ` Divide the array to search lngPos = (lngLow + lngUpper) / 2 ` Look for a match If flPartial Then ` If partial is specified then do a comparison ` on the substring since it should be a string If StrComp(Mid(varArray(pIndex, lngPos), 1, Len(varSearch)), _ varSearch, CompareMethod.Text) = 0 Then ` If we've found it then get out Return lngPos End If Else ` Check to see if its a string If varSearch.GetType Is GetType(System.String) Then If StrComp(varArray(pIndex, lngPos), varSearch, _ CompareMethod.Text) = 0 Then ` If we've found it then get out Return lngPos End If Else If varArray(pIndex, lngPos) = varSearch Then ` If we've found it then get out Return lngPos End If End If End If ` Check to see if its the last value to be checked If lngUpper = lngLow + 1 Then lngLow = lngUpper Else ` If we get to the lowest position then it's not there If lngPos = lngLow Then Return -1 Else ` Determine whether to look in the upper or ` lower half of the array If varSearch.GetType Is GetType(System.String) Then If StrComp(varArray(pIndex, lngPos), varSearch, _ CompareMethod.Text) = 1 Then lngUpper = lngPos Else lngLow = lngPos End If Else If varArray(pIndex, lngPos) > varSearch Then lngUpper = lngPos Else lngLow = lngPos End If End If End If End If Loop End Function End Class