With extended support for Visual Basic (VB) 6.0 nearing its end this year, many developers are looking for guidance on whether to upgrade, reuse, rewrite, or replace their legacy VB 6 code in favor of developing on supported software platforms. VB .NET, the designated successor of VB 6, looks more like C++ than traditional Basic, making the move from VB 6 less than ideal—but the move may be worth the effort.
The Microsoft .NET framework represents a significant redesign of the underlying VB 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 VB .NET to:
• Consolidate different code bases and software competency.
• Reduce upgrade and maintenance cost.
• Ensure ongoing training and official language support.
• Use 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, first you must understand the key components of .NET, the differences between VB 6 and VB .NET, and your options for migrating to .NET.
What Is .NET?
The .NET framework includes a set of class libraries that covers 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 then can use to request, update, and reconcile data between multiple data sources without needing database programming skills.
Many other components of the .NET framework offer definite value, including:
ASP.NET
ASP.NET is a framework used to create interactive Web pages well suited for remote monitoring and control applications. If the scarcity or location of test hardware requires you 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 revision of the application.
Web Services
Web services are 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
Multithreading APIs are 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, 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#.
Multiple Languages, One Run Time
When choosing a development language for a new project, many factors usually come into play including personal preference and expertise, legacy, and internal and external support for the language. Because certain languages may be appropriate for particular projects, many companies maintain code bases that include programming languages with different features and data types.
The .NET framework was designed to help address the challenge of supporting systems written in different programming languages. Instead of going directly down to native machine code, VB .NET code, for example, is compiled into an intermediate language called the Common Intermediate Language (CIL). Formerly referred to as the Microsoft Intermediate Language (MSIL), CIL is platform and processor independent and can be executed in any environment supporting the
.NET framework.
The Common Language Runtime (CLR) component of the .NET framework then translates CIL code into machine language. The CLR component supports more than 40 different programming languages, including VB .NET, C#, C++ .NET, PASCAL, COBOL, and various scripting languages.
Microsoft Visual Studio includes four of the most common .NET languages, one of those being VB .NET. 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.
How VB Has Changed
VB .NET is an evolution of VB engineered around built-in type safety; object-oriented functionality; and the capability to target Windows, Web, and mobile devices. While many things have changed, the overall VB 6 development experience essentially is 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:
• Object linking and embedding (OLE) container control
• Dynamic data exchange (DDE)
• Data access object (DAO) or raster document object (RDO) data binding
• Certain ActiveX/COM components
• Dynamic HTML (DHTML) applications
• ActiveX property pages
• 16-bit and 32-bit whole-number data types
Today, many test applications use 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.
The following examples compare how to interact with a 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
VB .NET also introduces a 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 tabbed property pages.
Figure 1. Visual Studio .NET Property Editor
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 otherwise could not be achieved in VB 6. With VB .NET, you can access more than 6,500 functions included in the .NET framework. These functions 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 an application upon completion, which minimizes memory leaks.
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 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 predefined 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 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. As a result, you can use the VB 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. Consequently, 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 the upgrade code alerting you to statements that 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. VB .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 applications more compatible with future versions of VB and provides 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, move to one of several languages specifically designed for test and measurement such as NI LabVIEW, NI LabWindows/CVI, Agilent VEE, or HTBasic.
VB 6.0 | VB .NET |
Standard EXE | Windows Application |
ActiveX DLL | Class Library |
ActiveX EXE | Class Library |
ActiveX Control | Windows Control Library |
ActiveX Document | No Equivalent; VB .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 |
Table 1. Software Components in VB 6 and Their Replacements in VB .NET
Source: Microsoft Developer Network
If your VB 6 applications work well and require little or no maintenance, then upgrading to VB .NET may not be worth the investment. 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 VB .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 invested now should pay off in dividends later when you need to maintain test systems or recruit new talent to develop them.
For More Information
1. “Help for Visual Basic 6 Users,” http://msdn.microsoft.com/en-us/library/kehz1dz1(VS.80).aspx
2. “Upgrading Applications in Visual Basic 6, http://msdn2.microsoft.com/en-us/library/dy163kx7(VS.80).aspx
3. “Language Changes for Visual Basic 6 Users,” http://msdn2.microsoft.com/en-us/library/skw8dhdd(VS.80).aspx
.NET Resources
1. Appleman, D., Moving to VB .NET: Strategies, Concepts, and Code, June 2001, www.apress.com
2. Clark, D., An Introduction to Object-Oriented Programming with Visual Basic .NET, 2002, www.apress.com
3. Halvorson, M., Microsoft Visual Basic .NET Step by Step, 2002, www.microsoft.com/mspress
4. Mojica, J., C# & VB.NET Conversion Pocket Reference, 2002, www.oreilly.com
5. Mojica, J., “Versioning VB 6 Components with VB .NET: An Excuse to Use VB. NET Today, ” Aug. 22, 2001, dotnet.oreilly.com/news/complus_0801.html
6. Reynolds, M., Crossland, J., Blain, R., and Wells, T., Beginning VB.NET, Second Edition, 2002, www.wrox.com
7. Roman, S., VB .NET Language in a Nutshell, Second Edition, 2002, www.oreilly.com
8. Troelsen, A., COM and .NET Interoperability, 2002, www.apress.com
About the Author
Wendy Logan is a product marketing engineer at National Instruments. Her current projects include outbound marketing and product strategy for Measurement Studio and LabWindows/CVI. Ms. Logan began work at National Instruments in 2004 after receiving a bachelor’s degree in computer science from Rice University. National Instruments,11500 N. Mopac Expwy., Austin, TX 78759, 512-683-9311, e-mail: [email protected]
June 2008