10 steps to migrate existing code to VB.NET
Converting existing VB code to Visual Basic .NET is not just a matter of loading it to the new version. Yes, VB.NET versions up to 2008 have their built-in migration tool, which does a good job converting your syntax. But that's just half of the work. Your project won't compile right away, you'll have a lot of issues ahead. If you weren't prepared, you might decide you didn't want to migrate after all.
Fortunately, you can prepare your code in advance. Actually, it is essential to work on your code before you load it in VB.NET.
There is one program that fulfills the tool recommendations below. The code analysis and VB.NET Compatibility Check features of Project Analyzer are designed to help in moving from your current VB version to VB.NET. Read more about upgrading from VB6 to VB.NET with Project Analyzer.
The migration process
- Decide whether to upgrade or not. There are program types that are best left in your current VB version. Projects that use UserControls, WebClasses, DHTML pages, VB's add-in model and other special techniques cannot easily be upgraded. Use a compatibility check tool to estimate what you can upgrade to VB.NET and how much work it will require. Alternatively, try to load your project in VB.NET and see the upgrade report for critical issues.
- Remove dead and duplicated code. Start by deleting all the code your programs don't use. It is not uncommon to have 30-40% dead code in a medium or large sized project. Migrating that code means extra burden and no benefits. Additionally, you should consider joining any duplicated functions to reduce the amount of code even further. — Now, how do you know what code is dead or duplicated? Of course you can try to find that out manually, but if your goal is to minimize work, we strongly recommend that you get a decent source code analyzer to find the dead and duplicated code.
- Upgrade problematic syntax and controls. Use of certain syntax and control versions will cause you a lot of trouble. Take the Data control or VB5 Common Controls controls (ComCtl32.ocx or ComCt232.ocx), for example. You must upgrade to ADO data control or MsComCtl.ocx and MsComCt2.ocx, respectively. And, if you happen to be still in VB3 or VB4, you must port it to VB6 first. Obsolete syntax like GoSubs need to get removed, and you even need to prepare your conditional compilation (#If statements). Fortunately, you don't have to upgrade everything, only those things that the VB.NET upgrade wizard doesn't handle. Use a compatibility check tool that recommends what to fix now and what to leave as is.
- Fix your data declarations. If you've used a lot of Variants and undeclared variables, you're almost doomed. Now it's your final chance to add those Option Explicit statements and add proper type declarations to every Dim statement. Not only will this make your code more robust and optimized - it will also enable the upgrade wizard to properly port your code. Control object variable declarations are of special importance if you don't feel like writing half of your method and property calls again. In addition, certain array and user-defined structure definitions are going to change. Use a source code analyzer that lists the declarations requiring a fix.
- Load your code in VB.NET. Let the upgrade wizard do its job. You'll get a large report detailing the found upgrade issues, both fixed and unfixed. You'll get comments in the code near places that need to get worked on. What you're not getting is a list of undetected issues, of course.
- Try to compile your project. It won't succeed. You'll have a long list of compile errors. Now it really starts to pay off to be properly prepared. The list would be much longer if you hadn't prepared in advance.
- Go back. You might want to go back to step 1, 2, 3 or 4, fix some issues, and then go to step 5 and run the upgrade wizard again.
- Compile & fix cycle. Fix all the compiler issues. Finally, it runs.
- Fix the commented issues. The upgrade wizard left comments in your code. Find them and do what they say. Now it's also beneficial to review a list of expected behavior changes, like events that won't fire.
- Test. Now you're back to daily life. Run and test your program to make sure it works as expected.
How do I learn this all?
Get a very small VB6 project to start with. Your project might consist of just 1 or 2 forms. You can make one by yourself, or use one of VB6's sample projects.
Download a free demo of Project Analyzer Enterprise Edition. This demo is fully functional for projects with max 10 source code files. Run Project Analyzer and analyze your small project with it. Follow the steps from 1 to 10.
VB.NET compatibility issues detected by Project Analyzer.