By Irina Medvinskaya
By now you’ve probably heard how .NET technology will affect the way Visual Basic programmers will develop. In our first look at VB.NET, we discussed the changes that are coming. In this article, we’ll discuss some of the language modifications necessary to make the step up to VB.NET.
Visual Basic.NET supports upgrading from VB6; however, if your application is written in any version prior to 6, you will have to upgrade it to VB6 prior to upgrading it to VB.NET.
Visual Basic.NET can be installed on a computer that has Visual Basic 6 installed, and both can run at the same time. Applications written in both VB6 and VB.NET can also be installed and executed on the same computer. Furthermore, components written in VB6 can be used in VB.NET projects.
Upgrading syntax and objects
Fortunately, there is a tool to assist in some of the modifications you must make to your programs and projects. However, not everything is automatic. In some cases, you’ll need to manually make the changes.
Microsoft Upgrade Tool
Microsoft provides the Visual Basic.NET Upgrade Tool, which takes care of many upgrade issues when moving from VB6 to VB.NET. Included as a part of VB.NET, the tool creates a new VB.NET project and leaves the existing project unchanged. The upgraded project can't be opened in VB6.
The tool runs automatically when you open VB6 projects in VB.NET and takes care of syntax changes, such as upgrading Integer to Short and Long to Integer data types and upgrading Variant to Object. The tool also converts Visual Basic forms to Windows Forms.
When the upgrade tool can't resolve the code differences, it automatically adds a To Do task in the new Task List window with an upgrade error message or a remark to alert developers to manual modifications that need to be made. Developers can navigate to the code statement by double-clicking the task.
As we mentioned, developers will need to make a number of modifications manually to convert VB6 projects to VB.NET. You must manually make these changes:
- Specify all properties and methods not relying on defaults, because for the most part, they are not available in VB.NET.
- Convert all arrays to zero-bound arrays. Declaring an array like Dim ThisArray(3) will create an array with exactly three elements 0 to 2 in VB.NET.
- Remove obsolete keywords, such as GoSub, Option Base, IsMissing, Let, Wend, VarPtr, ObjPtr, StrPtr, DefBool, DefByte, DefInt, DefLng, DefCur, DefSng, DefDbl, DefDec, DefDate, DefStr, DefObj, and DefVar. End While in VB.NET replaces the Wend keyword.
- Remove all code for forms resizing and stop using third-party resize controls since Windows Forms allows automatic resizing.
Tips for upgrading today's applications
To make sure your applications run the way you expect them to, follow these guidelines:
- Declare variables in the right places and watch out for the scope. VB.NET allows for block scope, which means you can declare variables inside blocks, such as If statements and loops, but these variables are available only inside that block. So if you plan to use variables throughout a procedure, make sure they are declared at the top.
- Declare all variables explicitly. VB6 allows declaring variables of different data types on the same line, but VB.NET doesn't. Also, in VB6, if you declare two variables but only specify the data type of the second, the first will be declared as Variant. In VB.NET, both variables will be declared with the second variable’s data type, so you have to watch out for those declarations.
- Explicitly declare all parameters to be either ByRef or ByVal. In VB6, the default for passing intrinsic data types is ByRef; for all others, it’s ByVal. In VB.NET, the default is ByVal for all data types.
- Use constant names instead of their underlying values. Use True and False instead of 0 and -1. In VB.NET, True is no longer equal to 1 but to -1, so using True and False instead of values will help porting your application to VB.NET easier.
- Get the application logic out of forms and modules and into classes and components. Since VB.NET is so different in many aspects, it may be easier to migrate classes to VB.NET than to migrate modules. The migration will be especially complicated if the application logic is intervened with the presentation layer, so encapsulating the application logic and business rules into your classes may be necessary.
- Avoid referencing the form controls directly in modules. In VB.NET, you have to declare those controls as Public Shared in order to reference them from other modules; however, doing so is not considered the best solution. Use property procedures for passing data instead.
- Use default values for all optional parameters because IsMissing is not supported in VB.NET.
Prepare for tomorrow by writing better code today
Microsoft has a number of additional recommendations for making the upgrade as seamless as possible. Developers can make the following manual changes to existing application code:
- Use early binding.
- Avoid Null propagation.
- Use ADO for data access.
- Avoid using the Double data type for storing dates.
- Avoid using fixed-length strings in user-defined types.
Follow these additional guidelines and you should have a fairly smooth transition:
- Avoid using Currency and Variant data types.
- Remove implicit object instantiation.
- Don't create new WebClasses and DHTML pages.
- Stop using OLE controls.
- Stop using DDE.
- Disable the Timer control by setting its Enabled property to False, instead of setting Interval property to 0.
- Stop using Form.PrintForm method.
- Use Twips Scale Mode in your Forms for the Forms' size to be upgraded properly. The Upgrade Tool will convert twips to pixels.
- Use TrueType or open-type fonts on your forms, such as Arial, so you don’t lose the formatting during the upgrade to VB.NET.
- Avoid using Shape and Line controls.
- Avoid using As Any in declarations.
- Avoid using Name property in run time.
If you need more information regarding VB.NET, check out these sources.
- Microsoft Visual Studio Next Generation
- Microsoft TechNet
- MSDN Online .NET Developer Center
- MSDN Magazine Online
- Microsoft Support Knowledge Base
- .NET Framework Resources on MSDN and microsoft.com
- .netBooks: Home For Microsoft .NET Reference
- Introducing the Microsoft .NETPlatform
By David S. Platt
Microsoft Press; ISBN: 073561377X
- VB.NET Programming with the Public Beta
By Billy S. Hollis, Rockford Lhotka
Wrox Press Inc; ISBN: 1861004915
- Introducing .NET
By James Conard, Patrick Dengler
Wrox Press Inc; ISBN: 1861004893
Visual Basic Programmer's Journal
By now you should have a pretty good idea of what you need to do in order to have your applications work the way you want under VB.NET. Next time, we’ll take a closer look at some of the new VB.NET features you might want to take advantage of.
Irina Medvinskaya is a programmer/analyst at Citigroup for the Treasury Systems department in New York.
What are your VB.NET plans?
Do you foresee a substantial conversion effort in order to move to VB.NET? What benefits will you be looking for? Send us an e-mail with your thoughts and suggestions or post a comment below.