Analyzing Your Visual Basic 6.0 Applications for Migration to .NET

Publish Date: May 28, 2008 | 68 Ratings | 1.06 out of 5 |  PDF

Overview

With the extended support for Visual Basic 6.0 ending this year, many application developers are looking for guidance on whether to upgrade, reuse, or replace their legacy Visual Basic 6.0 code in favor of developing on Microsoft-supported software platforms.
This whitepaper will discuss the difference between Visual Basic 6 and Visual Basic .NET and three options for transitioning to .NET.

Table of Contents

  1. What Is .NET?
  2. How VB Has Changed

The Microsoft .NET framework represents a significant redesign of the underlying Visual Basic programming language. For test applications, the new features help with writing distributed applications, organizing large programs, and achieving maximum performance.

Engineers want to migrate to Visual Basic .NET to:

  • Consolidate different code bases and software competency
  • Reduce upgrade and maintenance cost
  • Ensure ongoing training and official language support
  • Take advantage of the latest productivity enhancements and language features
  • Create or improve existing distributed applications, using the latest Web technologies

To effectively analyze if and which applications you should migrate to VB .NET, you must first understand the key components of .NET, the differences between VB 6 and VB .NET, and your options for migrating to .NET.

 

1. What Is .NET?

The .NET framework includes a set of class libraries that cover a number of common programming needs including file I/O, database connectivity, Web application development, and Windows services such as background printing and integrated e-mail. One class library automatically generates database-access objects, which you can then use to request, update, and reconcile data between multiple data sources without needing database programming skills. There are many other components of the .NET framework that offer particular value to the test engineer, including:

  • ASP.NET. Framework used to create interactive Web pages that are well-suited for remote monitoring and control applications. If the scarcity or location of test hardware requires you to be able to remotely monitor or control a system, creating a Web-based application may be the solution. Because ASP.NET does not require run-time engines to be installed on client computers, you do not need to update each client with every new revision of the application.
  • Web services. Self-contained modular applications that can publish information and be invoked across the Web. An example Web service could make available all of the calibration details for a transducer or instrument, eliminating the need to manually enter that data into your PC.
  • Multithreading APIs. Classes and interfaces that enable multithreaded programming. By using multiple threads in an application, you can ensure that your applications will be responsive to the user and, at the same time, be able to execute actions at regular intervals. For instance, by separating data acquisition (DAQ) and user interface tasks on different threads, you can ensure that user interface updates will not be delayed by calls to the DAQ hardware and vice versa.

The development environment most commonly used to access these .NET features is Microsoft Visual Studio, which includes various .NET programming languages – VB .NET, Visual C++ .NET, Visual C#, and Visual J#. Despite the name similarities between VB and VB .NET, VB .NET is fundamentally different from earlier versions of the language, so it requires some effort to learn.

 

Back to Top

2. How VB Has Changed

VB .NET is an evolution of VB that was engineered around adding built-in type safety; object-oriented functionality; and the ability to target Windows, Web, and mobile devices. While many things have changed, the overall VB 6 development experience is essentially the same. Because VB .NET is an object-oriented language, VB developers must learn new language rules, terminology, and ways to accomplish tasks such as debugging and deployment. For instance, VB .NET no longer natively supports features such as:

  • OLE container control
  • Dynamic data exchange (DDE)
  • DAO or RDO data binding
  • Certain ActiveX/COM components
  • DHTML applications
  • ActiveX property pages
  • 16-  and 32-bit whole-number data types

Today, many test applications make use of COM or ActiveX objects to control instruments, create user interfaces, and process test data. While Microsoft has replaced COM objects with .NET controls and .NET classes, it has provided functionality to upgrade certain VB 6 ActiveX controls, such as the Web browser, toolbar, richtextbox, and progress bar controls, to their VB .NET equivalents.

 

Visual Basic 6.0

Visual Basic .NET

Standard EXE

Windows application

ActiveX DLL

Class library

ActiveX EXE

Class library

ActiveX control

Windows control library

ActiveX document

No equivalent – Visual Basic .NET can interoperate with ActiveX documents

DHTML application

No equivalent – use ASP.NET Web application

IIS application (Web class)

No equivalent – use ASP.NET Web application

Source: Microsoft Developer Network

Table 1. Software Components in VB 6 and Their Replacements in VB .NET

 

Listing 1 compares how to interact with a digital multimeter (DMM) using a VB 6 COM object and a VB .NET class library. The .NET version of the code sample demonstrates some of the error-checking features of .NET and how to use .NET add-in components for I/O.

 

‘Example with VB 6.0

‘Open communication with the Fluke 45 and read
‘the frequency Fluke45 is an ActiveX control on the current form
Private Sub StartButton_Click()
  Fluke45.Open
  Fluke45.Write “FREQ”
  Fluke45.Write “val1?”
  FrequencyTextBox.Text = Fluke45.Read
End Sub

‘Notify the user if an error occurred
Private Sub Fluke45_OnError(ByVal ErrorCode As Long, ByVal ErrMsg As String)
  MsgBox ErrMsg
End Sub

=======================================
‘Example with VB .NET

Try
 ‘Open communication with the Fluke 45 and read
 ‘the frequency
 Dim fluke45 As New 
 NationalInstruments.VisaNS.MessageBasedSession(“ASRL1::INSTR”)
 fluke45.Write(“FREQ”)
 FrequencyTextBox.Text = fluke45.Query(“val1?”)
 Catch ex As Exception
 ‘Notify the user if an error occurred
 System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

Listing 1.Code Samples for Calling a COM Object in VB 6 and Using a Class Library in VB .NET

 

VB .NET also introduces a new method for setting the properties of a user interface control. With VB .NET, you set a control’s properties either through code or with the Visual Studio .NET property editor (Figure 1). This is a departure from ActiveX controls, which you use to set a control’s properties either through code or through tabbed property pages.

Figure 1.In Visual Studio, you can modify control properties through the .NET property editor instead of through ActiveX property pages.

 

Common Libraries, Functions, and Tools

The VB language itself has seen some significant changes that may leave experienced developers longing for the familiar, but VB .NET offers language enhancements that could otherwise not be achieved in VB 6. With VB .NET, you can access more than 6,500 functions included in the .NET framework. These functions all follow a consistent programming format. Once you learn to use one library, you can easily learn to use others. You can control memory leaks with another noteworthy new tool. Simply put, the garbage collection capabilities of VB .NET free all memory allocated to your application upon completion, which minimizes memory leaks.

 

Figure 2. With base classes in .NET, you can use .NET framework functions in your application.

 

With .NET, Microsoft no longer uses COM objects such as ActiveX. Fortunately, Microsoft developed a tool that lets you use COM objects in .NET languages. The tool creates a wrapper, called a “Runtime Callable Wrapper,” around the COM object to make it compatible with .NET languages. Through the wrapper, COM objects can call functions in .NET class libraries and vice versa.

Another helpful utility for migrating from VB 6 to VB .NET is the Visual Basic Upgrade Wizard. This tool cycles through pre-defined steps to convert VB 6 source code and project files into a format that can be reused in VB .NET. This utility automatically runs when you open a VB 6 project in a .NET-compatible version of Visual Studio (Visual Studio 2003, 2005, or 2008). Even though this tool tries to convert a working VB 6 application into a working VB .NET application, some parts of your project may not map directly. In this case, the wizard generates a report detailing how to make the final modifications to your project in VB .NET. Therefore, you can use the Visual Basic Upgrade Wizard as a quick tool to determine the amount of rework required to convert a VB 6 project to .NET.

Among the code you need to prepare for conversion are the data types you used in VB 6. Many engineers use the “variant” data type, which accepts any form of data. VB .NET enforces stricter type rules by replacing variants with objects (see Listing 1). Therefore, you need to manually search your VB 6 code for variants and change them to more stringently defined variables before you run the Conversion Wizard.

The Upgrade Wizard inserts comments into your upgrade code alerting you to statements that you need to change. These items, flagged as “TO DO” tasks, are associated with a Help topic in the Visual Studio documentation, which offers further information on how to modify your code.

In addition to replacing the variant data type, VB .NET eliminates the “goto on error” command. While such features make programming in Basic easier than in other languages, they also let you create unreadable code. VB .NET forces you to develop better programming habits, which leads to more maintainable code. Moreover, VB .NET introduces more robust error checking with the support of structured exception handling. Commonplace in the C++ world, Visual Basic .NET allows you to handle most of the errors users may encounter and enable the application to continue running with the Try...Catch...Finally syntax.

 

Transitioning to .NET

To develop test applications that take advantage of the .NET framework, you have three basic options:

  • Upgrade or Rewrite. Convert your VB 6 code to VB .NET code. Doing this makes your applications more compatible with future versions of VB and gives you access to .NET features, but it also requires rework.
  • Reuse. Write new applications in VB .NET but stay with VB 6 for existing applications that run critical tests and require maintenance but not upgrades.
  • Replace. Opt for another general-purpose language that supports .NET such as C++ or C#. Or, you can move to one of several languages specifically designed for test and measurement such as NI LabVIEW, NI LabWindows™/CVI, Agilent VEE, or HTBasic.

If your VB 6 applications work well and require little or no maintenance, then upgrading to VB .NET may not be worth the investment because porting an application to a new language can be time-intensive and may introduce new bugs into your code. If an application remains core to your test system and the life span of the application is sufficiently longer than the time required to upgrade the project, migrating to Visual Basic .NET or to another industry-supported language may be a logical step.

For new applications, you have to decide if you want to continue developing applications in VB 6, learn VB .NET, or switch to another programming language. While learning VB .NET syntax and programming requirements takes time, you probably need to do it eventually if you choose to stick with the Microsoft programming environment. With programming expertise, training, and support moving toward .NET or industry-specific languages, the safe bet for future application development may be to make the switch. The time investment now should pay off in dividends later when you need to maintain test systems or recruit new talent to develop them.

 

Additional Resources

Transition from Traditional NI-DAQ (Legacy) in Visual Basic 6 to NI-DAQmx in Visual Basic .NET
Visual Basic .NET Resource Page
Evaluate Measurement Studio for Visual Basic .NET

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit