Microsoft Cloud Authors: Nick Basinger, Kevin Benedict, Pat Romanski, Liz McMillan, Lori MacVittie

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Converting VB6 to VB.NET, Part I

A Look at Your Options and When to Use Them

If the VB6 program has variable names that match keywords in VB.NET, they'll be converted with '_renamed' appended to the declaration. The wizard doesn't always find where the variable is used, so the best option is to change the variable name before the conversion.

Don't bother with formatting code in the pre-conversion stage - the converter will do its own formatting during the conversion.

Well-Written Code Converts Well
I was around when Borland and then Microsoft released C++ compilers, and I was responsible for converting a lot of C code to C++. One lesson I learned was that well-written code converted far easier than poorly written code. So, the first task of a conversion effort should to clean up and simplify the original code. The following checklist contains some of the pre-conversion steps that will make the conversion easier - I will be covering all of these.

  1. Declare all variables.
  2. Give all variables explicit type, not variants.
  3. Do not worry about code formatting.
  4. Do not use 0 to disable timer, set enable = false instead (vb6 default is 0, enabled).
  5. Can't be perfect (bring to front).
  6. Do an internal test and release of a clean VB version before the final conversion.
  7. Declare only one variable per line.
  8. Remove dead code.
  9. Set the startup object to a form.
  10. Make sure the code compile. and runs before the conversion.
  11. Convert all arrays to 0 based.
  12. Remove all "as any."
  13. Convert VB1 - VB5 to VB6 first.
  14. Convert DAO and RDO to ADO.
  15. Do not use defaults when accessing control properties.
  16. Rename any VB.NET keywords in the VB6 code.

When I was doing the C to C++ conversions, one key quality indicator that I looked for was function prototypes. In C, prototypes were optional (and didn't even exist in the early days of C!). In C++, prototypes are mandatory. When I saw C code with prototypes, the conversion typically went well; when they were missing, other issues often came up during the conversion.

A similar quality indicator in VB6 is the declaration of variables and variable types. VB6 allows untyped variables in the form of variants. Actually, VB6 doesn't even require variables to be declared. In most cases, using variants is a bad programming practice; not declaring variables is always a bad programming practice.

So, the first thing to do when converting VB6 code to VB.NET is to add the Option Explicit to all code files, ensuring that all variables are declared. Next, specify all variable types, eliminating all variants that really only hold a single type. In many cases, it's obvious what type a variable should be, but incorrectly changing a variant that should be an Integer, Long, Single, or Double to a different type can cause bugs that are difficult both to detect and troubleshoot. This is one reason why I recommend a complete testing cycle after preparing VB6 code for conversion, but before doing the final conversion. If you can add more tests for corner cases in any math routines to your test suite, it would probably be worth it.

Another good practice is to declare one variable per line. Dim a, b, c As Integer creates two Variants and one Integer, not the three Integers you might expect. This error is more likely to cause problems in VB.NET than in VB6.

If your code is in an earlier version of VB, convert it to VB6 first. Next, if you use DAO or RDO to access databases, rewrite it to use ADO (more on this in the next article). VB.NET 2003 does a better job of converting VB than the original VB.NET, so if you're going to convert to .NET, it makes sense to skip VB.NET 2002 and convert straight to VB.NET 2003. Because of this, I won't cover converting VB.NET 2002, but most of the information given here will still apply if you choose that route.

After the upgrade, the new .NET project will be in its own folder, with the original VB6 project unchanged, so you can do as many trial runs as you like, and use the upgrade reports as guides to issues to correct before doing the final conversion. The update report is an html file listing files and the errors in each file.

VB6 and ActiveX Controls
Most of the basic VB6 controls will be upgraded by the wizard without any issues, as will many advanced and third-party controls. Even complex ActiveX controls like the MSChart and third party controls are likely to convert with only minor changes in syntax. For instance, the MSChart control converts and mostly behaves like the original. The biggest difference is that the Axis is accessed as a property in VB6, but this is done by using an accessor in VB.NET. For example, in VB6 the XAxis maximum value is set by the statement:

MSChart1.Plot.Axis(VtChAxisIdY,0).ValueScale.Maximum = 370

In VB.NET, it is set by the statement:

MSChart1.Plot.get_Axis(VtChAxisIdY,0).ValueScale.Maximum = 370;

As far as I can tell, this is the only property of MSChart that is treated with this minor difference. The wizard will automatically create a wrapper for any ActiveX controls used in the project. You can also create a wrapper for any ActiveX control you want to use in a .NET program by simply right-clicking on the toolbox, selecting add/remove items, and then choosing the ActiveX control from the COM tab of the Dialog Box. This task can also be accomplished by running the TlbImp.exe utility that comes with the free .NET SDK. One small difference: an "upgraded" ActiveX control is referred to by its original name when referenced in the program, but an ActiveX control added to a new .NET project gets an "ax" prefix. For instance, when upgraded from a VB6 project, MSChart stays MSChart; when added to a .NET project, it becomes axMSChart.

While complex components and ActiveX controls are moved to .NET via the use of wrappers, basic VB6 controls are upgraded to (replaced by) VB.NET controls.

More Stories By Dennis Hayes

Dennis Hayes is a programmer at Georgia Tech in Atlanta Georgia where he writes software for the Adult Cognition Lab in the Psychology Department. He has been involved with the Mono project for over six years, and has been writing the Monkey Business column for over five years.

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Early Bird Registration Discount Expires on August 31, 2018 Conference Registration Link ▸ HERE. Pick from all 200 sessions in all 10 tracks, plus 22 Keynotes & General Sessions! Lunch is served two days. EXPIRES AUGUST 31, 2018. Ticket prices: ($1,295-Aug 31) ($1,495-Oct 31) ($1,995-Nov 12) ($2,500-Walk-in)
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...