Global TMW:
Login  |  Register          Free Newsletter Subscription
Subscribe
Email
Print
Reprint
Learn RSS

Should you switch to VB .NET?

Visual Basic .NET looks more like C++ than traditional Basic, so making the move from VB 6 isn't easy—but it may be worth the effort.

Shelley Erickson, National Instruments -- Test & Measurement World, 12/1/2002

LISTING 1
What is .NET?
.NET Resources

Microsoft's .NET computing framework stands poised to help you speed test application development. With Visual Studio .NET (Microsoft's .NET programming package), you can write distributed applications, organize large programs, interact with multiple languages, and design for maximum performance. (See "What is .NET?" for more information.)

Visual Studio .NET includes four languages. Of these, Visual Basic (VB) programmers will most likely be drawn to the VB .NET option, but because VB .NET is fundamentally different from earlier versions of the language, it will take time and effort to learn. Before switching to VB .NET, you should consider your options. If you switch, you'll also need to evaluate whether you should migrate test applications written in VB 6.

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

  • Write new applications in VB .NET, but stay with VB 6 for existing applications that run critical tests and require maintenance but not upgrades.
  • Convert your VB 6 code to VB .NET code. Doing this will make your applications more compatible with future versions of VB and will give you access to .NET's features, but it also requires rework.
  • 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 LabView, Agilent Vee, LabWindows/CVI, or HTBasic.

If your VB 6 applications work well and require little or no maintenance, then you should not convert them to VB .NET. Porting debugged applications risks introducing new bugs into the code and can require time-consuming rework.

For new applications, you'll 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's syntax and programming requirements takes time, you'll probably need to do it eventually if you choose to stick with Microsoft's programming environment. (See ".NET resources " for a list of books and Web sites that can help you make the transition to VB .NET.)

Figure 1 In Visual Studio .NET, Microsoft replaces the ActiveX property pages with a .NET property editor.

How VB has changed

To let VB take advantage of the .NET framework, Microsoft built VB .NET from the ground up. The company transformed VB into an object-oriented programming language; thus, VB programmers must learn a new language with a new programming syntax. VB .NET no longer natively supports features such as:

  • OLE container control;
  • dynamic data exchange (DDE);
  • common-object model (COM) objects, including ActiveX components;
  • ActiveX property pages; and
  • 16-bit and 32-bit whole-number data types.

Today, few test applications use DDE to transfer test data, but many use COM or ActiveX objects to control instruments, create user interfaces, and process test data. Microsoft has replaced COM objects with .NET controls and .NET classes. Table 1 lists some of the software components in VB 6 and their replacements in VB .NET.

Listing 1 contains code samples for calling a COM object in VB 6 and for using a class library in VB .NET; the COM object and the class library provide programming interfaces for a DMM. The .NET version of the code sample demonstrates some of the error-catching features of .NET and how to use .NET add-in components for I/O.

VB .NET also introduces a new method for setting a control's properties. 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 let you set a control's properties either through code or through tabbed property pages (Figure 2).

Figure 2 ActiveX controls in VB 6 use property pages to set character-istics.

Class libraries add functions

Seasoned programmers will miss some of VB's familiar features, yet VB .NET offers useful replacements. VB .NET applies the same variable types, arrays, user-defined types, classes, and interfaces as Visual C++ and Microsoft's new language, Visual C# (both come with Visual Studio .NET), and it also includes several new tools.

For example, the .NET framework provides a set of base class libraries, which expose functions such as I/O, database connectivity, and other operating-system features to VB .NET programmers. Microsoft provides more than 6500 functions through .NET base class libraries, all of which follow a consistent programming format; once you learn to use one library, you can easily learn to use others. Figure 3 shows part of a base class library from which you can select a math function to add to your code.

Figure 3 Base classes in .NET let you add commonly used functions.

 

 

 

 

 

 

 

 

Another noteworthy new tool lets you control memory leaks. When a program opens, Windows allocates memory to that application. When it closes, the application should release all of the allocated memory. If it doesn't, the system will eventually run out of available memory. VB .NET includes a "garbage collection" feature that forces you to free memory when it's no longer needed, thereby minimizing 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 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.

VB .NET also has a "Conversion Wizard" utility that converts some VB 6 code to VB .NET code. Before you can run most VB 6 code through the conversion tool, you should manually prepare the code to account for differences between the languages. The more preparation you do, the fewer errors you'll have to correct in the converted code.

Among the code you'll 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. Variants make for easy, albeit sloppy, coding by not forcing you to specifically define a variable.

You can't use variants with VB .NET because the language replaces variants with objects (Listing 1). Therefore, you'll need to manually search your VB 6 code for variants and change them to more stringently defined variables before you run the Conversion Wizard.

When you run the conversion tool, it will flag the code portions that it can't upgrade and provide links to help you find more information. Remember, though, that just converting an existing application for VB .NET doesn't incorporate any .NET features. To add .NET features to an application, you'll have to write them in VB .NET.

In addition to replacing the variant data type, VB .NET also eliminates the "goto on error" command. While such features make programming in Basic easier than in other languages, they also let you create "spaghetti" code. VB .NET forces you to develop better programming habits, which leads to more maintainable code.

.NET's effects

Eventually, .NET will affect your work and, possibly, your career. Writing new applications in VB 6 may get your new test system running in less time than if you have to learn VB .NET, but if you stay with VB 6 too long, you risk falling behind those engineers who make the jump.

For more information

"Visual Basic Language Changes," Microsoft, Redmond, WA. msdn.microsoft.com/library/en-us/cpguide/html/cpconvisualbasiclanguagechanges.asp.

Table 1. Components in VB 6.0 and VB .NET
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


Author Information
Shelley Erickson is a software product manager at National Instruments. She holds a BS in computer science from the University of Missouri-Rolla. Before moving to product marketing, she worked as an applications engineer supporting National Instruments' software and hardware products.

 

LISTING 1

‘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

 

What is .NET?

The .NET framework is a set of technologies, not all of which will be incorporated into new applications. It consists of five main components, three of which hold interest to test engineers:

  • Web services. Self-contained, self-describing, modular applications that you can publish, locate, and invoke across the Web. Web services perform functions, which can be anything from simple requests to complicated instrument controls. Once you deploy a Web service, other applications (and other Web services) can invoke it. A 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. This type of Web service could let you automatically get the latest information on an instrument you're using. Another Web service could let you get data from a remote instrument and download the data to your computer.
  • Development tools. The application development environment (ADE) that you use to develop .NET applications, most commonly thought of as Visual Studio .NET, which includes VB .NET, Visual C++ .NET, and Visual C# .NET.
  • Clients. Software components that give PCs and other devices access to information over the Internet or local networks. For test engineers, clients might include data such as a calibration schedule for test equipment.

The remaining two components are called "experiences" and ".NET servers," but these probably won't affect your test work.

The .NET framework also provides over 6500 class libraries that make Windows functions available to your application. Class libraries cover such functions as file I/O and data collection and Windows services such as background printing and integrated e-mail.

One class library automatically generates database-access objects; you can use these objects to interact with a database without having to learn database-programming details. In another example, the ".NET Remoting" set of class libraries let you to create an application that runs on other parts of the network—without learning TCP socket programming.

Write in many languages

The .NET framework lets you write applications in multiple languages more easily than ever before. Microsoft has developed its common language runtime (CLR), a compilation layer of the .NET framework, which compiles code from more than 25 different languages into code that the PC can run. .NET applications no longer run in native machine code because Microsoft abandoned Intel x86 code in favor of Microsoft Intermediate Language (MSIL), which runs in the CLR.

The CLR can handle traditional languages such as Cobol, Fortran, and Pascal; object-oriented languages such as the new Visual C# and Component Pascal; and scripting languages such as Jscript and Perl. These languages can "talk" to one another through a set of standard programming interfaces that Microsoft calls the Common Language Specification (CLS).

The more than 25 languages supported by the CLR share a hierarchy of classes that provide basic services. In addition to supporting multiple languages, the CLR framework also yields hardware-independent applications. Visual Studio, the development environment for .NET applications, compiles source code into MSIL, which is independent of hardware and operating systems. The CLR then executes MSIL and translates the MSIL code into machine language.

 

.NET Resources

  • Appleman, Dan, Moving to VB .NET: Strategies, Concepts, and Code, Apress, Berkeley, CA, June 2001. www.apress.com.
  • Clark, Dan, An Introduction to Object-Oriented Programming with Visual Basic .NET.Apress, Berkeley, CA, 2002. www.apress.com.
  • Halvorson, Mark, Microsoft Visual Basic .NET Step by Step, Microsoft Press, 2002. www.microsoft.com/mspress.
  • Mojica, Jose, C# & VB.NET Conversion Pocket Reference, O'Reilly & Associates, Sebastopol, CA, 2002. www.oreilly.com.
  • Mojica, Jose, "Versioning VB 6 Components with VB .NET: An Excuse to Use VB. NET Today," August 22, 2001. dotnet.oreilly.com/news/complus_0801.html.
  • Reynolds, Matthew, Jonathan Crossland, Richard Blain, and Thearon Wells, Beginning VB .NET, 2nd ed., Wrox Press, Chicago, IL, 2002. www.wrox.com.
  • Roman, Steven, VB .NET Language in a Nutshell, 2nd ed., O'Reilly & Associates, Sebastopol, CA, 2002. www.oreilly.com.
  • Troelsen, Andrew, COM and .NET Interoperability, Apress, Berkeley, CA, 2002. www.apress.com.
  • "Visual Basic .NET Upgrade Guide." Microsoft. msdn.microsoft.com/vbasic/techinfo/articles/upgrade/guide.asp.

Email
Print
Reprint
Learn RSS

Talkback

We would love your feedback!

Post a comment

» VIEW ALL TALKBACK THREADS

Related Content

Related Content

 

By This Author

There are no other articles written by this author.

Sponsored Links



 
Advertisement
SPONSORED LINKS

More Content

  • Blogs
  • Podcasts

Blogs

  • Rick Nelson
    Taking the Measure

    August 18, 2008
    Home fusion
    What do you do in your spare time? Watch the Olympics? You might consider building your own nuclear ...
    More
  • Martin Rowe
    Rowe's and Columns

    August 11, 2008
    Grachanen wins NCSLI award
    At last week's NCSL International Workshop and Symposium, Chris Grachanen was awarded the NCSLI Educ...
    More
  • » VIEW ALL BLOGS RSS

Podcasts

Advertisements





NEWSLETTERS
Click on a title below to learn more.

Test Industry News (3 Times Per Month)
Machine-Vision & Inspection (Monthly)
Communications Test (Monthly)
Design, Test & Yield (Monthly)
Automotive, Aerospace & Defense (Monthly)
Instrumentation (Monthly)
Resource Center E-Alert (Monthly)
©2008 Reed Business Information, a division of Reed Elsevier Inc. All rights reserved.
Use of this Web site is subject to its Terms of Use | Privacy Policy
Please visit these other Reed Business sites