Welcome!

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
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...
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...
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...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...