Click here to Skip to main content
15,902,938 members
Articles / Web Development / ASP.NET
Article

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 559.1K   17.2K   95   104
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):
    VB.NET
    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:
    VB.NET
    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.

Improvements

  • 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> =
    (
        "Len"
        ParentExp<out exp>    (.  exp += ".Length"; .)
        |
        "True"                (.  exp = "true"; .)
        |
    

Acknowledgments

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.

License

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.

License

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
CEO
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

 
GeneralRe: GBVB Pin
NormDroid28-Apr-03 8:44
professionalNormDroid28-Apr-03 8:44 
GeneralRe: GBVB Pin
vbinfo28-Apr-03 23:09
vbinfo28-Apr-03 23:09 
GeneralRe: GBVB Pin
dog_spawn29-Apr-03 11:23
dog_spawn29-Apr-03 11:23 
GeneralRe: GBVB Pin
vbinfo1-May-03 4:30
vbinfo1-May-03 4:30 
GeneralRe: GBVB Pin
Kevin McFarlane1-May-03 5:14
Kevin McFarlane1-May-03 5:14 
GeneralRe: GBVB Pin
Eric Astor13-May-03 15:12
Eric Astor13-May-03 15:12 
GeneralRe: GBVB Pin
Stephen Quattlebaum27-Jun-03 9:43
Stephen Quattlebaum27-Jun-03 9:43 
GeneralRe: GBVB Pin
vbinfo28-Jun-03 9:18
vbinfo28-Jun-03 9:18 
I think you are wrong

I know .net which includes c# and vb.net.I cannot say i know them well but from what you are saying you dont know .net very well.

There are things that vb.net can do that c# cant and viceversa.

Many of these choices are politics made but anyway

What i am saying is not coming from me but from the .net team (i think they know it more than you and me.)

Many boolshit have been published allover the net saying that c# is faster than vb.net and so on
well those many times were wrong benchmarks

1)
Visual Basic .NET Compiler
All Framework compilers generate Intermediate Language (IL) that is later compiled to native code by the Just-In-Time (JIT) compiler. The same IL executes exactly the same on the common language runtime no matter which language was originally used. Runtime differences between Visual Basic .NET code and equivalent code in another language are caused by the respective compilers generating different Intermediate Language.


2)
Comparing compilers requires an understanding of the default behavior of each compiler to ensure that equivalent options are being used. Similar code segments compiled with dissimilar compiler options can have drastically different performance characteristics. But as will be shown in the remainder of this section, Visual Basic .NET code performs identically to C# code when both are compiled with equivalent options.

3)
Debugging and nop Instructions
Visual Basic .NET allows you to set breakpoints on non-executing lines of code such as End If, End Sub, and Dim statements. To facilitate this popular debugging technique, the compiler inserts nop instructions as placeholders for the non-executing lines of code (since non-executing lines are not translated into IL instructions). The nop instruction is a "no operation" instruction-it does not perform any meaningful work yet can consume a processing cycle.

You can observe this if you launch Visual Studio .NET, create a new Visual Basic .NET application, compile it using the default Debug configuration, and then view the assembly with the MSIL Disassembler (Ildasm.exe).

.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello World"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method Module1::Main
By default, the Visual Basic .NET compiler (vbc.exe) does not generate nop instructions. They are only generated when the /debug option for the compiler is explicitly set, which is exactly what the Debug configuration in Visual Studio .NET does. When you compile using the Release configuration in Visual Studio .NET, the /debug option is not used so the nop instructions are not generated.

In contrast, the C# compiler does not produce as many nop instructions even when compiling with the /debug option. Since the Visual Basic .NET and C# compilers do not behave equivalently when used with the /debug option, you should compile using the Release configuration mode when comparing compilers, especially for performance comparisons.

Integer Overflow Checking
Unlike the other Framework SDK compilers, vbc.exe generates overflow checks for integer operations by default (for Visual Studio .NET users, this includes both the Debug and Release configurations).

As a result, benchmarks results have occasionally been posted that do not accurately compare the performance of Visual Basic .NET to other languages, because the benchmarks do not take into account integer overflow checking and other default compiler settings. The following code is an example:

Visual Basic .NET C#
Public Shared Sub RunLoop()
Dim I As Integer
Dim max As Integer = 500000000
Dim sum As Integer = 0

For I = 1 To max
sum += 1
Next

End Sub

public static void RunLoop()
{
int i;
int max = 500000000;
int sum = 0;
int loopLimit = max;
for (i = 1; i <= loopLimit; i++)
{
sum += 1;
}
}



The code segments look equivalent (the extra C# variable loopLimit is used to match Visual Basic .NET behavior, which is to copy max to a temporary local variable). Both methods require two integer addition operations-one for the increment of the sum variable and one for the increment of the loop counter. By default, the Visual Basic .NET compiler will generate the IL instruction add.ovf for these addition operations. The add.ovf instruction includes an overflow check and throws an exception if the sum exceeds the capacity of the target data type. By contrast, the default output of the C# compiler is the IL instruction add, which does not include an overflow check. Using these default compiler options would mean a performance advantage for the C# version of RunLoop because of the overflow checks done by the Visual Basic .NET.

If an overflow is considered an error in your application, the add.ovf instruction results in safer code. Otherwise, integer overflow checking can be disabled for the Visual Basic .NET compiler using the /removeintchecks option. Alternatively, integer overflow checking can be enabled for the C# compiler using the /checked option. These options can also be controlled in Visual Studio .NET using the project properties dialog box.

An accurate comparison of the two RunLoop versions requires comparing the IL generated with equivalent compiler options for overflow checking. The following graph shows the average number of seconds that RunLoop executed in an informal performance test (on a modestly equipped computer) for both the Visual Basic .NET and C# compilers, with and without integer overflow checks. As can be clearly seen, equivalent compiler options yield equivalent performance results.



These identical performance results are not surprising if you look at the IL generated by each compiler, which is almost identical for the two RunLoop versions. The table below shows the resulting IL when integer overflow checking is disabled for both compilers. The only difference is the order of the instructions on lines IL_0008, IL_0009, and IL_000a (the only implication is that the Visual Basic .NET version has an extra integer value on the stack while max is being copied to a temporary variable).

Tip for Reading IL: The common language runtime is stack-based. A two-operand operation such as add pops the two top values from the stack and adds them. In the left column, line IL_000e pushes the contents of a local variable (sum) onto the stack. The next line pushes the 4-byte integer constant 1 onto the stack. The next line pops the two values, adds them, and pushes the result on the stack. The next line, IL_0011, pops the result from the stack and stores it in a local variable (sum again).

vbc.exe-generated IL csc.exe-generated IL
.method public static void RunLoop() cil managed
{
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] int32 I,
[1] int32 max,
[2] int32 sum,
[3] int32 _Vb_t_i4_0)
IL_0000: ldc.i4 0x1dcd6500
IL_0005: stloc.1
IL_0006: ldc.i4.0
IL_0007: stloc.2
IL_0008: ldc.i4.1
IL_0009: ldloc.1
IL_000a: stloc.3
IL_000b: stloc.0
IL_000c: br.s IL_0016
IL_000e: ldloc.2
IL_000f: ldc.i4.1
IL_0010: add
IL_0011: stloc.2
IL_0012: ldloc.0
IL_0013: ldc.i4.1
IL_0014: add
IL_0015: stloc.0
IL_0016: ldloc.0
IL_0017: ldloc.3
IL_0018: ble.s IL_000e
IL_001a: ret
} // end of method Class1::RunLoop

.method private hidebysig static void RunLoop() cil managed
{
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] int32 i,
[1] int32 max,
[2] int32 sum,
[3] int32 loopLimit)
IL_0000: ldc.i4 0x1dcd6500
IL_0005: stloc.1
IL_0006: ldc.i4.0
IL_0007: stloc.2
IL_0008: ldloc.1
IL_0009: stloc.3
IL_000a: ldc.i4.1
IL_000b: stloc.0
IL_000c: br.s IL_0016
IL_000e: ldloc.2
IL_000f: ldc.i4.1
IL_0010: add
IL_0011: stloc.2
IL_0012: ldloc.0
IL_0013: ldc.i4.1
IL_0014: add
IL_0015: stloc.0
IL_0016: ldloc.0
IL_0017: ldloc.3
IL_0018: ble.s IL_000e
IL_001a: ret
} // end of method Class1::RunLoop



Unique Visual Basic Constructs
There are a number of constructs that are unique to the Visual Basic language. These include Modules, Handles, Optional Parameters, and Late Binding. Used correctly, these constructs can provide significant developer productivity enhancements over other languages that you can use with the .NET Framework. In addition, these constructs provide compatibility with Visual Basic 6 code. This section will explore the internal operation of these constructs so that you can determine when they are appropriate in your development.

I could go on and on but i dont see the point.
I strongly suggest that you widen you horizon as I have.

to conclude I am just angry that many c# guys have a wrong attitude and they have to face the fact that vb.net is on the same level as c# unless microsoft decides to go one way instead of the other.(unfortunately they are doing it already )with generics and so on.



CHILL OUT!!!!!
VBNETUK

vbnetuk@yahoo.co.uk
GeneralRe: GBVB Pin
Stephen Quattlebaum28-Jun-03 14:13
Stephen Quattlebaum28-Jun-03 14:13 
GeneralRe: GBVB Pin
vbinfo29-Jun-03 21:59
vbinfo29-Jun-03 21:59 
GeneralRe: GBVB Pin
Stephen Quattlebaum30-Jun-03 5:22
Stephen Quattlebaum30-Jun-03 5:22 
GeneralRe: GBVB Pin
vbinfo29-Jun-03 22:04
vbinfo29-Jun-03 22:04 
GeneralRe: GBVB Pin
Kevin McFarlane1-May-03 5:03
Kevin McFarlane1-May-03 5:03 
GeneralRe: GBVB Pin
The Limey5-May-03 14:36
The Limey5-May-03 14:36 
GeneralRe: GBVB Pin
NormDroid28-Apr-03 8:43
professionalNormDroid28-Apr-03 8:43 
General...and there was much rejoicing... Pin
Chris Maunder27-Apr-03 9:42
cofounderChris Maunder27-Apr-03 9:42 
GeneralRe: ...and there was much rejoicing... Pin
Daniel Turini27-Apr-03 10:21
Daniel Turini27-Apr-03 10:21 
GeneralRe: ...and there was much rejoicing... Pin
Paul Selormey27-Apr-03 18:02
Paul Selormey27-Apr-03 18:02 

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.