Click here to Skip to main content
15,499,958 members
Articles / Web Development / ASP.NET
Posted 26 Apr 2003


95 bookmarked

GBVB - Converting VB.NET code to C#

Rate me:
Please Sign up or sign in to vote.
2.38/5 (95 votes)
27 Apr 200310 min read
A tool and a method for VB.NET to C# source code conversion

GBVB - Converting VB.NET code to C#

“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.

Why convert?

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:

  1. The VB.NET code is still not strongly type-checked. It still makes some type conversions that may be dangerous. Try this code, with 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
  2. Most C# programmers find VB.NET syntax ugly and cumbersome. I know this is a matter of personal taste, but it is a good reason for code migration. If you do not feel comfortable using a language, it may be better learn to either love it, or get rid of it.
  3. C# features several compile-time code checking, besides strong typing. Access uninitialized variable, assign values to a variable and never user it, or declare a variable and do not use it, create unreachable code, and you will get an error or a warning from the compiler. VB.NET silently compiles this kind of code.
  4. Since it is easier writing a C# parser (I will write about this later in this article), there are and there will always be more for C# code analyzing and rewriting, while VB.NET will receive much less attention in this area.
  5. Because of some VB.NET ambiguities, sometimes VB.NET code can be slower than the C# equivalent.

Not Possible to Write a Perfect VB.NET to C# Converter

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:

  • A parser needs type information for disambiguation, e.g., when you see the code 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.
  • Some code using the 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.

Approaches for Code Conversion

  • Anakrino! Well, this is a reasonable idea, until you really try it. Just a tip: do you know the VB compiler does not put your comments on the final assembly? There are funnier things, too, but I will let you discover them by yourself.
  • Regular expressions. Come on, you can come up with something better than this. Again, a tip: try to write a recursive regular expression.
  • Write a parser. That is what I did. It is not so much work as it appears, if you keep yourself writing a simplified LL(1) grammar and if you use a nice tool, such as COCO/R for C#.
  • Do everything by hand. I have done that, and I can tell you: GBVB will prevent RSI like no other tool.

Goals and Restrictions

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:

  1. Garbage in, Garbage out: if you give the converter some garbage code, you will have more garbage.
  2. This is a typing-saving tool: GBVB will do repeatable most hard and repeatable work, but you still will need to do “brains” part. Some code will not be translated and sometimes you will get an error on the output: if needed, comment the affected code. The converted code does not even need to compile, but it should be faithful to the original VB.NET code.
  3. It should be very useful for converting my own code: if it is useful for you too, hey, it is your lucky day! Nah, I am just joking: actually, GBVB could translate nearly 90% of my code, so you will be very unlucky if GBVB do not for you.
  4. This should be an easy-to-code tool. Fast to code, too. As such, I hope you do not find that my coding style produces excessive commenting on the source code at a point that makes it hard to read.
  5. This is not a pretty printer (it is more like an ugly printer): it does generate indented code, but it does not generate highly organized code. By goal #2, it is up to you to organize it as you wish. Pressing Ctrl-K F on the generated code sometimes helps.
  6. I explicitly decided not to use heuristics, because of goal #2 and #4.
  7. No VB runtime functions will be converted to the .NET Framework “equivalent” ones, e.g., 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))
  8. On Error Goto / On Error Resume Next will only be part of GBVB over my dead body. See Goal #1.
  9. The Goto Statement: See Goal #8.
  10. The REM is an abomination used only by some distorted minds. There is the one char line commenting, did you know?
  11. Most array code will have trouble while being translated.
  12. Some VB.NET syntax keywords and features are not supported, like Handles, inline array declarations. Goals #2, #3, #4, and pure laziness.
  13. I made it using Visual Studio .NET 2003. I do not even have Visual Studio .NET 2002 on my machine anymore.

Using the tool

Image 1

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.

Converting from VB6 code

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.

Some advices before migrating

  • Only migrate solid, working code. Create a rigorous unit testing set (you already have this, right?) and only then migrate your code. This way, you can be sure your code runs the way it is supposed to run.
  • You will need to change some things before migrating, because some features are not support by either C# or GBVB. While still working on the VB.NET code, I recommend you to change the following things:
    • Get rid of your optional parameters.
    • Use Option Strict On and Option Compare Binary.
    • Classic On Error error handling is not supported in C#. Change this code to exception handling.
    • Modules are not supported, but 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.
    • Change Select Case with conditions and exception filters, if you use them.
    • Finally, remove all the VB runtime functions and use only the .NET framework equivalent ones. Do you remember the VB.NET weirdness I mentioned? Well, having a sound unit testing set will ease things to you.
    • If GBVB has trouble to migrate some statement, comment it, and migrate it manually. You will notice the trouble because GBVB will often stop the code migration on a specific statement.
    • After migrating, do some code cleaning. Normally, C# will give you hundreds of healthy warnings about your “perfect” VB.NET.


  • It could be an add-in for VS.NET. For now, I recommend you to add it to the Tools menu.
  • GBVB is only able to migrate “full” code files. Code snippets do not migrate nicely.
  • Do not complain of my commenting style, I know that so many comments like I did can make the code hard to read! Besides this, there is a mix of Portuguese and English words on the identifiers, which makes looking at the sources a rather “globalized” experience. But, if you are experienced with COCO/R, the code is plain obvious and easy to follow.
  • GBVB do not support the “one line” If Then, nor multiple statements on the same line.
  • Remove the LL(1) warnings, and the “Not Expression” annoying messages.
  • Create an option for the brace position. It is easy to change the Util.OpenBlock and the Util.CloseBlock methods for this.
  • It is very easy to add some automatic translation of VB.NET runtime functions if you like to live in danger. In a future version, this will be implemented as optional features. On the VBNET.ATG file, change the Expression compiler production and add specific translation for your code. Like the sample below, where I convert a Len(expression) to Expression.Length (if you didn’t use it before, see the power of COCO/R!):
    Expression<out string exp> =
        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.

The Standard Disclaimer

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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Brazil Brazil
I develop software since I was 11. In the past 20 years, I developed software and used very different machines and languages, since Z80 based ones (ZX81, MSX) to mainframe computers. I still have passion for ASM, though no use for it anymore.
Professionally, I developed systems for managing very large databases, mainly on Sybase and SQL Server. Most of the solutions I write are for the financial market, focused on credit systems.
To date, I learned about 20 computer languages. As the moment, I'm in love with C# and the .NET framework, although I only can say I’m very proficient at C#, VB.NET(I’m not proud of this), T/SQL, C++ and libraries like ATL and STL.
I hate doing user interfaces, whether Web based or not, and I’m quite good at doing server side work and reusable components.
I’m the technical architect and one of the authors of Crivo, the most successful automated credit and risk assessment system available in Brazil.

Comments and Discussions

GeneralMy vote of 1 Pin
Grumpyman16-Jan-15 13:19
MemberGrumpyman16-Jan-15 13:19 
GeneralMy vote of 1 Pin
AliSufyan5-Apr-13 22:37
MemberAliSufyan5-Apr-13 22:37 
GeneralMy vote of 1 Pin
Mic299920-Jul-12 9:12
MemberMic299920-Jul-12 9:12 
GeneralMy vote of 2 Pin
Mic299920-Jul-12 9:11
MemberMic299920-Jul-12 9:11 
GeneralC# to VB.Net, can who guide me? Pin
Watcharakorn Wanich27-May-11 10:05
MemberWatcharakorn Wanich27-May-11 10:05 
RantThis tool is not bad, but your wrong about VB.NET Pin
vsvlad2-Apr-11 9:10
Membervsvlad2-Apr-11 9:10 
GeneralMy vote of 3 Pin
RusselSSC27-Jan-11 13:26
MemberRusselSSC27-Jan-11 13:26 
GeneralMy vote of 1 Pin
Brijesh Vadukia19-Jul-10 4:14
MemberBrijesh Vadukia19-Jul-10 4:14 
GeneralFantastic Alternative, Works Perfect Pin
Anshul R10-Jun-10 20:34
MemberAnshul R10-Jun-10 20:34 
GeneralMy vote of 1 Pin
Moshe B7-Mar-10 5:13
MemberMoshe B7-Mar-10 5:13 
GeneralMy vote of 1 Pin
TAFIN12-Dec-09 18:58
MemberTAFIN12-Dec-09 18:58 
Generaltotally boghas!!! Pin
TAFIN12-Dec-09 18:49
MemberTAFIN12-Dec-09 18:49 
GeneralHi Pin
Ravenet29-Oct-09 7:13
MemberRavenet29-Oct-09 7:13 
GeneralMy vote of 1 Pin
Can SARIGÜL1-Aug-09 11:10
MemberCan SARIGÜL1-Aug-09 11:10 
QuestionHave you tried the SharpDevelop free online converter? Pin
psmartt126-Mar-09 15:03
Memberpsmartt126-Mar-09 15:03 
AnswerRe: Have you tried the SharpDevelop free online converter? [modified] Pin
ToolmakerSteve27-Apr-09 19:34
MemberToolmakerSteve27-Apr-09 19:34 
GeneralMy vote of 1 Pin
bfk4-Feb-09 7:05
Memberbfk4-Feb-09 7:05 
GeneralRe: My vote of 1 Pin
Andy *M*26-Mar-09 23:20
MemberAndy *M*26-Mar-09 23:20 
GeneralMy vote of 1 Pin
vikramkharbanda17-Dec-08 18:59
Membervikramkharbanda17-Dec-08 18:59 
GeneralRe: My vote of 1 Pin
Andy *M*26-Mar-09 23:19
MemberAndy *M*26-Mar-09 23:19 
RantGood Idea... Bad Implementation... Bad Computer Language Assumptions stated in the article! Pin
Destiny7774-Nov-08 16:03
MemberDestiny7774-Nov-08 16:03 
RantRe: Good Idea... Bad Implementation... Bad Computer Language Assumptions stated in the article! Pin
ToolmakerSteve27-Apr-09 19:50
MemberToolmakerSteve27-Apr-09 19:50 
AnswerRe: Good Idea... Bad Implementation... Bad Computer Language Assumptions stated in the article! Pin
Destiny7774-Mar-11 10:32
MemberDestiny7774-Mar-11 10:32 
GeneralThe conversion process Pin
Member 47088016-Aug-08 17:21
MemberMember 47088016-Aug-08 17:21 
GeneralFirst line of article is wrong Pin
hogan7-Aug-07 5:23
Memberhogan7-Aug-07 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.