Converting between C# and VB.NET

While I'm a huge fan of learning and using different programming languages โ€“ whether to train your brain to consider different potential solutions to a problem or to improve your job security and your ability to roll with the punches as the employment & technology landscapes shift โ€“ you will no doubt have noticed that the vast majority of my posts contain C# code.

A lot of my early programming (both work and fun) was in BASIC (whether BBC BASIC, Level II BASIC on the TRS-80 or GW-BASIC) and some of the more involved internal projects I've worked on at Autodesk have used one of VB's various incarnations. But over the years I've also invested a serious amount of time working with C & C++, which have somehow led to a deep, irrational attachment to curly braces and semi-colons. ๐Ÿ™‚

Anyway, the aim behind most of my posts is to demonstrate the use AutoCAD's managed API from a mainstream .NET language, and so I've focused most of my energy on providing new samples in one particular language rather than spending time converting between them (which I tend to leave as an exercise for those readers who prefer VB.NET).

I do still get the occasional request for code in VB.NET rather than C#, and it occurred to me that it was a good time to mention a few of the resources I usually point people to, to help them with their code conversion efforts.

Firstly, here are some online conversion tools:

Carlos Aguilar Mares' Code Translator (C# <-> VB)
Kamal Patel's Convert C# to VB.NET (C# โ€“> VB, VB โ€“> C# on its way)
developerFusion's .net code converter (C# <-> VB)

In the past I've made use of the first two of these resources via a Visual Studio plugin letting you to copy C# code and paste it as VB, although I didn't end up bringing this across when I started using VS 2008. Kamal Patel also has a downloadable version of his C# to VB convertor for offline use (provided with full source-code).

Language choice in ReflectorWhen offline I tend to use .NET Reflector, which also allows you to decompile a built (and hopefully not obfuscated ๐Ÿ™‚ .NET assembly to C# or VB, among other language options (one of which I hadn't even heard before).

I have found none of these options to be perfect, especially when attempting to convert complex code: it's pretty rare that some tidy-up isn't needed on the output, whether related to the way casts are performed, event handlers are added/removed or some other issue. But they do provide a good starting point.

If you have experience with these or other code conversion tools โ€“ whether you use them successfully on a regular basis or have knowledge of their respective shortcomings โ€“ then please post a comment. I'm sure many of you have more real-world experience of using online code translation tools which would be relevant for this blog's readership.

Oh, and in case you want to hear the latest news of the strategy around the evolution of VB.NET and C# โ€“ in terms of the languages' "co-evolution" - I recommend watching this post on Channel 9. It's likely to make you more confident in investing in either C# or VB, knowing that Microsoft is committed to evolving both languages to have similar new features.

One response to “Converting between C# and VB.NET”

  1. Kean

    SharpDevelop (1 used to have a C# <--> VB.Net conversion tool built into the last version I used, but I don't know about the latest version.

    They have a converter online at 1 which does C#->VB.NET, C#->Boo, VB.NET->C#, VB.NET->Boo

    Nick

  2. Kean Walmsley Avatar

    Nice! Thanks, Nick. ๐Ÿ™‚

    Kean

  3. Stephen Preston Avatar
    Stephen Preston

    FYI - The developerFusion translator Kean quotes is based on the SharpDevelop source. They credit it on their webpage.

  4. Kean Walmsley Avatar

    And I should probably mention that Stephen pointed me to that site, in the first place (thanks, Stephen :-).

    Kean

  5. Hi Kean,

    I tried the converter by Kamal and the developer fusion one, I found the latter to be much better.

    Example: Kamal's converter doesn't take care of C# keyword 'as'. The developer fusion one, converts the 'as' keyword in the equivalent form: Trycast()

  6. Stephan Bartl Avatar

    Just wanted to point out this older blog-article, since it is also on this topic:
    blog.jtbworld.com/2005/12/convert-c-to-vbnet-code.html

  7. change to c#.

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalculate.Click
    Dim conversionFactor As Double

    If Conv_InchCm.Checked Then
    conversionFactor = 2.54
    ElseIf conv_CmInch.Checked Then
    conversionFactor = 1 / 2.54
    ElseIf Conv_YrdMetres.Checked Then
    conversionFactor = 0.9144
    ElseIf conv_MetresYrd.Checked Then
    conversionFactor = 1 / 0.9144
    ElseIf conv_MilesKm.Checked Then
    conversionFactor = 1.609344
    ElseIf conv_KmMiles.Checked Then
    conversionFactor = 1 / 1.609344
    End If

    Dim value As Double
    Double.TryParse(txtDisplay.Text, value)

    value = value * conversionFactor

    txtDisplay.Text = value.ToString

    End Sub
    End Class

  8. Unfortunately this isn't a place to request code conversions. I suggest trying one of the resources listed in this post, or asking your question via a forum somewhere (perhaps StackOverflow.com).

    Let me know if I've misunderstood your intent.

    Kean

  9. Some time these on line free code conversion tools gives a very common error : -- line 1 col 1: EOF expected
    while my code has no issue. But these tools helps me a lot.
    I have also converted full project with the help of these tools.
    some time ago i have discover a new free online tool:

    defercode.com/tools/csharp-to-vb.aspx

    you can also try this tool.

    thanks
    Ankur

  10. Some of the instruments suggested by you don't exist anymore.
    I suggest to try the offline solution Ispirer toolkit

Leave a Reply

Your email address will not be published. Required fields are marked *