![]() |
Languages »
C# »
General
Intermediate
GBVB - Converting VB.NET code to C#By Daniel TuriniA tool and a method for VB.NET to C# source code conversion |
C#, VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
�GBVB (Goodbye VB) is an amazing tool that flawlessly converts any portion of VB.NET code to C# code in a fraction of seconds.� I would love to be able to say this. If you are looking for such a tool, forget about it. You will not be able to find it, and I will explain why later in this article.
Isn�t .NET all about language interoperability? Why would someone ever need to convert VB.NET code to C#? Well, even though the languages are fully interoperable, there are some good reasons for this conversion:
Option Strict On, for an example (both are true): Dim x As String
If x Is Nothing Then
Console.WriteLine("X is Nothing")
End If
If x = "" Then
Console.WriteLine("X is an empty string")
End If
It is not possible to write such a perfect tool because VB.NET syntax allows a programmer to write ambiguous code. VB.NET syntax ambiguities follow in two categories:
ambiguous(3), you need type information to know if this is a function call or an array access. When translating to C#, you�ll need to either use ambiguous(3) or ambiguous[3]. Actually, this does not make it impossible to write such a tool, but it makes it harder.
Object data type can only be disambiguated at runtime, e.g., ambiguous(3) will only be resolved on runtime. Actually, it can change from one call to other. This kind of code is impossible to translate. One could use some heuristics to determine the runtime data type at parsing time, but it would be hard and yet not 100% effective. With the previous knowledge in mind, I decided to make a tool that converts some �well-behaved� VB.NET code to C#. Let us define some goals for GBVB, sorted by priority:
Mid(x) will not be converted to x.Substring. Although easy, as I show you later, this would introduce several bugs and would need revision anyway, without the compiler errors to help you. Don�t you believe me? Run this code, just for fun: Dim x As String
If x Is Nothing Then
Console.WriteLine("X is Nothing")
End If
If x = "" Then
Console.WriteLine("X is an empty string")
End If
If Mid(x, 1, 1) = "" Then
Console.WriteLine("Mid X is an empty string")
End If
If Len(x) = 0 Then
Console.WriteLine("Len(X) == 0")
End If
Console.WriteLine(x.Substring(0, 1))
On Error Goto / On Error Resume Next will only be part of GBVB over my dead body. See Goal #1.
Goto Statement: See Goal #8.
REM is an abomination used only by some distorted minds. There is the one char line commenting, did you know?
Handles, inline array declarations. Goals #2, #3, #4, and pure laziness.
Look at the screen shot: if you cannot figure out by yourself how to run and use this tool, you should not be programming. Try something easier, there may be some exciting jobs for you on the food market. Alternatively, keep with VB.
A direct conversion is sometimes possible, but the VB Upgrade Wizard will do a much better job. Therefore, I do strongly recommend you to upgrade the code to VB.NET, run it, test it, fix it, and only then convert it to C# code.
Option Strict On and Option Compare Binary.
On Error error handling is not supported in C#. Change this code to exception handling.
Friend classes that have only Shared methods can easily substitute them. Bear in mind that often code on Modules have global variables and is not thread safe, especially if migrated from VB6 (VB6 code ran in STA, so it was not susceptible to this kind of problem), so you can see this as a good opportunity to break it in smaller classes.
Select Case with conditions and exception filters, if you use them.
Expression<out string exp> = ( �Len� ParentExp<out exp> (. exp += �.Length�; .) | "True" (. exp = "true"; .) |
This tool was only possible because of the C# version of COCO/R, by Hanspeter Moessenboeck.
I used the version modified by Pat Terry, available here.
Actually, I slightly changed the version, because all the parsers I write need to be thread safe and deal with accented chars. This specific parser needed also a delegate on the commenting parser, because COCO/R by default ignores comments, which is a good thing, but not in this case. Therefore, everything that is working ok has to be credited to Pat & Hanspeter, and every bug may have been introduced by me.
I did not put my COCO/R changed sources on the ZIP files because I cannot assure you if my modified version is fully compatible with the original, only that it suits my needs. If you are interested on the sources, mail me and I will send you.
You can use all the code wrote by me in this article (everything but COCO/R, which is subject to its own licensing) and distribute it freely, as soon as you keep the Copyright notice. If you create some derived work, put a link back to this article, citing me as the author of the original work, as I have did on the Acknowledgments section. You cannot sell nor license this code without my written authorization, but you can make sell or license the code you converted using it.
As I said before, I tested this on my machine and it works fine. Use it at your own risk: if you use it, you can lose data, profit, have hardware problems, cause radioactive contamination and start a nuclear world war. However, for me, it works fine and never had such a problem.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Apr 2003 Editor: Heath Stewart |
Copyright 2003 by Daniel Turini Everything else Copyright © CodeProject, 1999-2009 Web13 | Advertise on the Code Project |