Welcome!

Microsoft Cloud Authors: Pat Romanski, Liz McMillan, Lori MacVittie, Elizabeth White, Yeshim Deniz

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 deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
Machine learning has taken residence at our cities' cores and now we can finally have "smart cities." Cities are a collection of buildings made to provide the structure and safety necessary for people to function, create and survive. Buildings are a pool of ever-changing performance data from large automated systems such as heating and cooling to the people that live and work within them. Through machine learning, buildings can optimize performance, reduce costs, and improve occupant comfort by ...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
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...
Poor data quality and analytics drive down business value. In fact, Gartner estimated that the average financial impact of poor data quality on organizations is $9.7 million per year. But bad data is much more than a cost center. By eroding trust in information, analytics and the business decisions based on these, it is a serious impediment to digital transformation.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Predicting the future has never been more challenging - not because of the lack of data but because of the flood of ungoverned and risk laden information. Microsoft states that 2.5 exabytes of data are created every day. Expectations and reliance on data are being pushed to the limits, as demands around hybrid options continue to grow.
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 ...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...