views:

4667

answers:

11

Does anyone know a way to convert from VB6 code to C#?

Is there a tool that can do this for me?

+16  A: 

VisualStudio offers (or at least offered) a wizard to do a conversion from VB6 to VB.NET (which could then be converted to C# with a bit of work, possibly helped by #develop's VB.NET <-> C# converter), but when last I used it, for anything non-trivial there was a lot of manual work needing to be done so I suspect you're probably better rewriting or porting by hand if this is a large and/or important application.

theraccoonbear
+6  A: 

Open your project with a new version of Visual Studio, convert your code to VB.Net and then download .Net Reflector to help you with the C# transformation.

Good luck!

sebastian
This answer is misleading as the conversion from VB6 is filled with both subtle and gross changes in behavior. Do NOT do this. It is better to have a methodical plan for the conversion that treats .NET as a separate platform then rely on any of the conversion tools.
RS Conley
yet, I believe it is a better approach that just having a method...
sebastian
Good Luck is right! For any non trivial program there is a better chance of winning the lottery than producing anything useful with this method. :)
Kevin Gale
.Net Reflector does a poor job converting from VBNet to C#, there are tons of labels, gotos, etc created by the process. Using a semantic converter (Like one found in CSharpDeveloper or such) is much better.
Kris Erickson
+2  A: 

Artinsoft does just this, especifically the Visual Basic Upgrade Companion.

However, even after using the VBUC there's still some parts that of the system that needs to be migrated/proofed by hand. But it's usually a much smaller set of the original problem. And some of the migration issues have been resolved thanks to experience with past migrations.

Artinsoft is the same company that built the wizard that ships with Visual Studio, mentioned in theraccoonbear's post. However, if I'm not mistake the wizard only migrates VB6 to VB.Net.

Full disclosure: I work for Artinsoft

Esteban Brenes
Esteban. are there limits on the number of times the vbuc can be run on files? I'm referring to this statement on your website "Licensing is per application, allowing to run the VBUC several times upon the same VB6 files. "
kjack
If I'm not mistaken you'll basically be time limited (generally 1 year) during which time you can migrate as many times as you want. The license itself is quite permissive once you get to the actual execution. In most cases ArtinSoft just relies on the client doing right by the license terms.
Esteban Brenes
Thanks for the information, Esteban, that sounds a lot better than the quote I gave which is suggestive of a quite restrictive limit.
kjack
Artinsoft are now offering a free license for their product, capable of upgrading 10,000 lines of code http://www.artinsoft.com/artinsoft-renews-its-efforts-to-ease-the-transition-from-microsoft-visual-basic-6-to-microsoft-net-framework.aspx
MarkJ
+9  A: 

It might come across as a little bit cheeky but your brain might be the best tool to use. Could be worth the re-write.

Maybe you don't need to porting it. Could the VB6 be turned into a COM component? Port to COM, call from C#, home in time for Judge Judy.

Derek Smyth
"[...]home in time for Judge Judy"priceless!
theraccoonbear
A: 

One line at a time...

Joshua
Sorry, I couldn't resist.
Joshua
+2  A: 

There are 2 free ones that I use:

http://www.carlosag.net/Tools/CodeTranslator/

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

Scott and the Dev Team
The second works better than the first one
sebastian
+4  A: 

The short answer is that VB6 and VB.NET (and consequently C#) are separate language but related languages. There are many subtle (Integer being Int32 instead of Int16) and gross differences (graphics, form, and printing engines) between the two platforms. You need to treat this as if you are converting to a completely different platform.

The issues is explored more fully here

How to Switch a Large App from VB6 to VB.NET

RS Conley
+2  A: 

As someone who has done this a bunch of times, this is not an easy process. You can use the VB6 to VB.Net tool as stated in this answer, and then use either Reflector or SharpDevelop to convert to C#. With the SharpDevelop conversion, a few caveats. It screws up all the array references and thinks that they are function calls, and all the logical operators are converted to bitwise logical operators (And becomes & not &&). With Reflector you lose a bunch of stuff. Also the Visual Studio converter fails on a lot of large projects, just hangs and never completes.

Once you have got your code converted into C#, you have to start the real work. The conversion gets you at best 50% of the way there, you have to fix a ton of stuff (you will see your code littered with TODO's), refactor a ton of stuff, and at the end you are left with C# that is a representation of your VB6 -- unless you have very nice VB6 code not a place you really want to be. Also all of your code with be littered with the VB helpers rather than using proper DotNet functions (all the string functions are helpers rather than class objects, for examples0. If you used Variants at all those all have to be rewritten. If you used a lot of API calls, they tend to need rewritting.

In the end you will get a base, but converting a large project (20-30 forms, 30 classes, 30 modules) can take several man months. Rewritting from scratch, however, may take twice as long and you lose all of your business logic. So, it can be done (I have done it with 3 or 4 large projects), but there is no panacea, no silver bullet, and any tool that says it will do it for you alone, is lying.

Kris Erickson
if your real work only begins when the code is converted to c# why wouldn't you leave it in vb.net? Were the people doing the cost benefit analysis making an informed decision?
kjack
Oh, there is a ton of work to do if you convert only to VB.Net, but there is no point in doing that work in VB.Net if you are going to translate to C#, fix it in the language you plan to end up in. We ported a few projects from VB6 to VBNet, but our developers are a lot more productive in C#.
Kris Erickson
Thanks, I understand what you mean now
kjack
+1  A: 

One question to ask yourself is, how important is it to have C# rather than VB.NET? If you're converting a large application, perhaps you should just upgrade to VB.NET, which will be cheaper.

There are some features in VB6 with no exact equivalent in C# (the error handling, late binding etc). Ask yourself why it's worth the manual effort to convert those, when you could go to VB.NET which does support them.

And if you don't believe me (and no-one's voted for my answer, sob sob!) maybe you'll believe Francesco Balena and Dan Appleman (in a book Moving to VB.NET) who agree with me.

MarkJ
A: 

At GreatMigrations, we do this by hand -- we use our hands to develop a VB6/COM compiler. The compiler reads a VBP (or group of related VBPs) and referenced COM libraries and constructs a "semantic model". This model is an extremely detailed set of symbol tables and opcode streams that fully and accurately describe all information structures defined and operations performed by the source system.

The tool then applies a series of algorithms that modify and restructure the model to make it more compatible with .NET and optionally applies custom transformations; for example to replace COM components with .NET classes. The transformation rules are designed by people and created "by hand" to meet their unique requirements.

Finally, the model is "executed" but instead of allocating the data model and executing the operations, we author the file structures, data model, and operations in the notation of the target platform. We can author both VB.NET and C# using this approach. We also use this process to migrate ASP sites to ASPX, (we also have a multi-dialect FORTRAN-to-C product)

The approach does not produce production ready software on the first try, but it can be improved and made to produce "better" software by modifying the configuration of the tool and re-running the translation process. The advantage of our tool is its repeatability, speed, and flexibility which facilitates an agile tool-assisted-rewrite methodology. IMO, migration teams that leverage our approach will benefit from allocating a lot more resources to evaluating alternatives for redesign, and development process improvements rather than to re-gathering functional requirements, hand coding, and testing. They will ultimately enjoy a more maintainable system and a much more faithful reproduction of the original application functionality after the migration as well as greater control and visibility during the migration.

A demo edition of our tool is available as a 14-day trial and will process codes up to 10K LOC.

mark
A: 

To migrate vb6 code , a tool that help to understand the existing code could be useful, for example VBDepend is a useful to understand the vb6 code base.

Issam