Microsoft Cloud Authors: Pat Romanski, Andreas Grabner, Nick Basinger, Kevin Benedict, Liz McMillan

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
The Internet of Things is clearly many things: data collection and analytics, wearables, Smart Grids and Smart Cities, the Industrial Internet, and more. Cool platforms like Arduino, Raspberry Pi, Intel's Galileo and Edison, and a diverse world of sensors are making the IoT a great toy box for developers in all these areas. In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists discussed what things are the most important, which will have the most profound e...
In today's enterprise, digital transformation represents organizational change even more so than technology change, as customer preferences and behavior drive end-to-end transformation across lines of business as well as IT. To capitalize on the ubiquitous disruption driving this transformation, companies must be able to innovate at an increasingly rapid pace.
Here are the Top 20 Twitter Influencers of the month as determined by the Kcore algorithm, in a range of current topics of interest from #IoT to #DeepLearning. To run a real-time search of a given term in our website and see the current top influencers, click on the topic name. Among the top 20 IoT influencers, ThingsEXPO ranked #14 and CloudEXPO ranked #17.
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
According to Forrester Research, every business will become either a digital predator or digital prey by 2020. To avoid demise, organizations must rapidly create new sources of value in their end-to-end customer experiences. True digital predators also must break down information and process silos and extend digital transformation initiatives to empower employees with the digital resources needed to win, serve, and retain customers.
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, provided an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. 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 settl...
Contextual Analytics of various threat data provides a deeper understanding of a given threat and enables identification of unknown threat vectors. In his session at @ThingsExpo, David Dufour, Head of Security Architecture, IoT, Webroot, Inc., discussed how through the use of Big Data analytics and deep data correlation across different threat types, it is possible to gain a better understanding of where, how and to what level of danger a malicious actor poses to an organization, and to determin...
@CloudEXPO and @ExpoDX, two of the most influential technology events in the world, have hosted hundreds of sponsors and exhibitors since our launch 10 years ago. @CloudEXPO and @ExpoDX New York and Silicon Valley provide a full year of face-to-face marketing opportunities for your company. Each sponsorship and exhibit package comes with pre and post-show marketing programs. By sponsoring and exhibiting in New York and Silicon Valley, you reach a full complement of decision makers and buyers in ...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
LogRocket helps product teams develop better experiences for users by recording videos of user sessions with logs and network data. It identifies UX problems and reveals the root cause of every bug. LogRocket presents impactful errors on a website, and how to reproduce it. With LogRocket, users can replay problems.