Chapter 6. Moving from VB 6 to VB 2005

To take the plunge and move your current VB 6 application to VB 2005 requires more than just a cursory overview of the capabilities of the language. Each type of application currently deployed in your environment warrants different considerations. In this chapter, I will discuss some of the factors you need to consider before upgrading your current application to VB 2005. I will also discuss various upgrade strategies that you can take should you decide to use VB 2005. This chapter will end with a look at using the Code Advisor for Visual Basic 6.0 and the Visual Basic Upgrade Wizard to upgrade your VB 6 application to VB 2005.

Migrate, Replace, Rewrite, or Reuse?

Once an organization has decided that a certain application no longer meets its business needs and that doing nothing is no longer an option, modernization comes into play.[1] There are at least four ways to approach the modernization of a VB application that should be considered. The deciding factors are:

  • The quality of the application code

  • The business value of the application

Quality in this case is about the suitability of the application in business and technical terms and should be assessed in accordance with the following parameters:

Current effectiveness of application

Generated errors, number of workarounds, and level of support needed.

Stability and completeness of core business rules

Will the application logic remain the same in the foreseeable future? An underlying assumption in this paper is that the current software asset is a valuable one. If the business model is going to change substantially, then this assumption has to be called into question. In practice, the code is often the only repository of business rules and these are scattered throughout the code. Thus any attempt to “start from scratch” needs to reconstruct and document the requirements captured in the current code and take these requirements as the starting point for the negotiation of new requirements.

Stage of the lifecycle

In the earlier stages of its lifecycle, an application will likely map closely to its functionality requirements, although the platform could be obsolete.

Development environment

The development team and the environmental capabilities required to successfully deliver a modernization project need to be assessed. Here, the developer’s knowledge regarding the application source code, the target technologies, and the resolution of modernization issues identified during the code assessment are crucial. In general, it is recommended that developers executing the project be fully trained in VB 2005. Additionally, other factors such as the existence of test cases must be considered.

The business value of the application is another important consideration and this will depend to a considerable degree on its uniqueness. If the quality of the application is poor and there is comparable functionality available in a third-party software package, it makes sense to replace it.

There are four broad modernization options—migrate, re-use, rewrite, or replace—any one of which can be the right choice either for a complete application or for parts of an application. Figure 6-1 shows how the decision factors correlate with the modernization path.

Migrate

If the VB application meets current business needs and its quality is good, chances are the application can be effectively modernized to continue to meet the needs of the business in the future. In such cases, a migration process can be applied and then functionality and business reach can be added as needed. In this chapter, when we refer to a migration or an upgrade, we are referring to an automatically assisted migration using the Microsoft Visual Basic Upgrade Wizard that is integrated in Visual Studio .NET (see “Upgrading VB 6 Applications,” later in this chapter).

Modernization options chart
Figure 6-1. Modernization options chart
Reuse

There are two possibilities here, one in which the application is centered on a third-party package/DBMS already, and the other in which the business has developed its own application from scratch. If the VB 6 application portfolio is largely centered on a third-party package, then the best way to move forward may be to upgrade to the latest version and use wrapping techniques to provide the required reach and other functionality improvements. For in-house applications, consider wrapping the application pieces and integrating them with new development.

Rewrite

The key asset here is the business rules and data structures; the application is the problem. Application mining and analysis of code logic and data structures is required to provide the starting point for the rewrite.

Replace

Look for a suitable package or outsource. Be prepared to make changes to the business model to meet the package half way.

Making the Right Decision

Upon the initiation of the project you should prepare a feasibility analysis that provides an assessment of the business and technical quality of the application. The following series of checklists presents some of the questions that you should consider when choosing one of the alternatives.

Migrate

Below is the checklist for choosing to migrate:

Existing application fulfills current business needs
Moderate functionality changes needed in existing application
High operational costs of existing application
Need to migrate to the .NET Framework for strategic reasons
Future vision includes the use of web services or web access
Stable code base and a test suite that certifies it
Resources needed to maintain or amend applications on existing platform are difficult to find

Reuse

Below is the checklist for choosing to reuse:

Business rules satisfactory
Low operational costs of existing application
Simple web access required, allowing a wrapping solution
Have resources to keep core Visual Basic 6.0 application maintained
Off-the-shelf software central to existing, rely on a third party to support and maintain

Rewrite

Below is the checklist for choosing to rewrite:

Functionality does not meet business needs
No off-the-shelf solution comes close to meeting needs
Poor quality code in existing platform and high maintenance costs
Can afford time, cost, and disruption involved
Need to use the Microsoft .NET Framework for strategic reasons
Future vision includes the use of web services

Replace

Below is the checklist for choosing to replace:

Application significantly out of line with business needs
Willing to make changes to business model to fit off-the-shelf solution or availability of off-the-shelf solution that closely fit your business requirements
Can afford time, cost, and disruption involved

The preceding questions can apply to complete applications or to discrete parts of applications. Typically a large application will require use of more than one modernization alternative. When deciding the best path for a particular part of an application, bear in mind that many developers will invariably say that rewriting your application is the best solution if you need to upgrade it, because they usually feel they can write it better the second time, armed with the benefit of hindsight. Certainly if the application is poorly designed, rewriting it can be a good option because it provides an opportunity to do it right. However, examining the business case for upgrading, rewriting, replacing, or leaving the application in Visual Basic 6.0 always provides some interesting insights.

If the application already supports your business needs, doesn’t require enhancements to its functionality, and if you already have support staff trained in VB 6, then leaving the application in VB 6 is a good option. Nevertheless, your organization needs to assess the risks of this approach in light of current lifecycle guidelines from Microsoft and the opportunities that the VB 2005 and .NET framework offer to your organization.

If there is a business need to move the application to VB 2005, then there is a need to look more closely at rewriting versus upgrading. Upgrading the application using the VB 6 to VB 2005 migration tool is a cost-effective way to migrate your applications. One popular reason for moving an application to VB 2005 is to either web-enable the application, or to enhance an existing web-enabled application with ASP.NET features such as tracing, flexible state management, scaleable data access, and improved performance. As mentioned previously, rewriting sometimes yields an improved application. The downside is that the development cost will be much greater than upgrading.

There are some benefits to rewriting. Rewriting allows you to correct a poor design, and COM objects can be replaced with .NET objects that are more scaleable and don’t require registration during deployment. The flipside of this is that upgrading is much quicker and COM objects can be replaced with .NET objects after the upgrade has taken place.

In brief, you have to decide on how to move forward with your modernization project. If you decide that the best solution is to leave the application in VB 6, then you are done! On the other hand, if you have assessed that the best solution is to rewrite your application, then the best piece of advice is to make sure that you follow an accepted development methodology and that you really look back at the issue your current application has to make sure you can leverage that knowledge when moving forward. If you think your current application and its source code have value, and that by moving it to .NET you can extend its lifecycle, then you have decided that automatically assisted migration is the best solution for your code. Finally, you may decide to go for a combination of the above solutions, as is the case for most modernization projects.

Using COM Objects in VB 2005

One of the primary reasons VB 6 programmers and their companies are reluctant to migrate to VB 2005 is the huge investments they have made in developing COM components. However, COM components continue to be supported in .NET. In the following sections, you will see how you can use legacy COM components in your VB 2005 applications.

The most direct way to use COM objects in VB 2005 is to use COM Interop. Although applications that run under the .NET Framework can only work with .NET components, .NET allows you to use your existing COM components by means of a Runtime Callable Wrapper (RCW). When you use RCW to interact with a COM object, an assembly is used as a wrapper for the COM object. The RCW acts as a bridge between the unmanaged code (the COM object) and managed code (your .NET application), and all communications with the COM object are routed through this class (see Figure 6-2).

Runtime Callable Wrapper
Figure 6-2. Runtime Callable Wrapper

Tip

COM objects are unmanaged code, therefore you need to remember to free up their resources when they are no longer in use.

To illustrate the support of COM in .NET, let’s consider a simple example.

Suppose you want to display a PDF file in a VB 2005 Windows application. To do so, you can make use of the Adobe Acrobat Browser Document control (which is a COM object) installed on your system (the component is installed on your computer when you install the Adobe Acrobat Reader).

First, create a new Windows application. To use the Adobe COM component, look for it and select it from the list of COM components on your system. To see the list, click the COM tag of the Add Reference dialog in Solution Explorer in Visual Studio 2005, as shown in Figure 6-3. Click OK.

Drag and drop the Adobe Acrobat 7.0 Browser Document, which is now located in the Toolbox under the All Windows Forms tab, onto your Windows Form (Form1, unless you have renamed it). The result is shown in Figure 6-4.

Add a COM component to your project
Figure 6-3. Add a COM component to your project
Filling the form with the Adobe COM component
Figure 6-4. Filling the form with the Adobe COM component

To display a PDF document using the control, double-click on the Windows Form and code the Load event as shown in Example 6-1.

Example 6-1. Form1 Load event handler
Private Sub Form1_Load( _
	ByVal sender As System.Object, _
	ByVal e As System.EventArgs) _
	Handles MyBase.Load, _
		
	MyBase.Load
	AxAcroPDF1.LoadFile("C:MiniReader.pdf")
End Sub

There isn’t much difference in coding; your COM component is used much like a .NET component.

Tip

Besides exposing using COM components in a .NET application, you can also use .NET Components in a COM application via the COM Callable Wrapper (CCW). The CCW is used to marshal calls between managed and unmanaged code (see Figure 6-5), thereby allowing COM applications to make use of .NET components.

COM Callable Wrapper
Figure 6-5. COM Callable Wrapper

Figure 6-6 shows what happens when the application is run.

Using the COM component
Figure 6-6. Using the COM component

Not only does COM interop make it easy to use COM components in your VB 2005 application, it also does away with the error-prone task of installing and registering COM components on target computers through a new feature known as RegFree COM (Registration-Free COM).

Warning

RegFree COM runs only on Windows XP and later releases of the Windows operating system.

Using RegFree COM, you can deploy an application that uses a COM component without registering it on the user’s machine, thus avoiding the notorius collection of problems commonly referred to as “DLL Hell.” RegFree COM even allows you to run multiple versions of a COM component on the same machine.

Tip

RegFree COM works by automatically generating a manifest from the COM component’s type library and component registration on the developer’s machine. Therefore, while it is not required to install the component on the end users’ machines, a copy must be registered on the developer’s machine.

To enable use of RegFree COM, all COM components referenced in Visual Studio 2005 now have a new Isolated property (see Figure 6-7). If you set Isolated to true, the component can be deployed through ClickOnce, and Visual Studio 2005 will automatically do all the work to deploy the COM component onto the target machine (without needing to register it on the target machine).

Setting the Isolated property for RegFree COM
Figure 6-7. Setting the Isolated property for RegFree COM

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.

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).

The Code Advisor toolbar
Figure 6-8. The Code Advisor toolbar
An application in VB 6
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).

Viewing the FixIts report
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).

Customizing Code Advisor
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.

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.

The Visual Basic Upgrade Wizard
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.

The upgraded VB project
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.

The content of the _UpgradeReport.htm file
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.

Tip

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.

Summary

In this chapter, you have been introduced to several factors you need to consider before deciding whether or how to enhance your VB applications to meet future business needs. The various checklists provided here and others available from Microsoft will help you decide whether to migrate, replace, rewrite, or reuse an existing VB 6 application. You have also learned how you can continue to use COM objects—legacy or otherwise—in a VB 2005 application. Finally, you have seen how two tools provided by Microsoft (Code Advisor for VB 6 and Upgrade Wizard) can help you upgrade your applications from VB 6 to VB 2005, once you’ve decided to do so.



[1] The following section is an excerpt from the “Designing an Application Migration Strategy for Visual Basic 6.0 to Visual Basic .NET” whitepaper authored by ArtinSoft, published on MSDN at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/appmigrationstrat.asp.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.211.70