What is the best C# to VB.net converter?

96,458

Solution 1

If you cannot find a good converter, you could always compile the c# code and use the dissasembler in Reflector to see Visual Basic code. Some of the variable names will change.

Solution 2

Telerik has a good converter that is based on SharpDevelop that has worked pretty well over the years, though it has not been updated in years (due to it being based on SharpDevelop).

I've recently come across a roslyn based converter as well. I don't know how well it works or how well maintained it is, but as it's open source you can always fork it and update it as needed.

Solution 3

I currently use these two most often:

http://converter.telerik.com/

http://www.carlosag.net/tools/codetranslator/

But have also had some success with these others:

http://converter.atomproject.net/

http://www.dotnetspider.com/convert/Csharp-To-Vb.aspx

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Solution 4

SharpDevelop has a built-in translator between C# and VB.NET. Is not perfect thought (e.g. the optional values in VB.NET doesn't have an equivalent in C#, so the signature of the converter method must be edited), but you can save some time, as you are making all operations inside an IDE and not a webpage (copy C# code, paste, hit button, copy VB.NET code, paste on IDE :P )

Solution 5

I think the best thing to do is learn enough of the other language so that you can rewrite by hand, there's some quite difficult differences in certain aspects that I'm not sure a converter would handle very well. For example, compare my translation from C# to VB of the following:

public class FileSystemEventSubscription : EventSubscription
{
    private FileSystemWatcher fileSystemWatcher;

    public FileSystemEventSubscription(IComparable queueName, 
        Guid workflowInstanceId, FileSystemWatcher fileSystemWatcher) : base(queueName, workflowInstanceId)
    {
        this.fileSystemWatcher = fileSystemWatcher;
    }

becomes

Public Class FileSystemEventSubscription
    Inherits EventSubscription  
    Private myFileSystemWatcher As FileSystemWatcher
    Public Sub New(ByVal QueueName As IComparable, ByVal WorkflowInstanceID As Guid, ByVal Watcher As FileSystemWatcher)
        MyBase.New(QueueName, WorkflowInstanceID)
        Me.myFileSystemWatcher = Watcher
    End Sub

The C# is from the Custom Activity Framework sample, and I'm afraid I've lost the link to it. But it contains some nasty looking inheritance (from a VB point of view).

Share:
96,458

Related videos on Youtube

Florian
Author by

Florian

Working with VB,VBA,C#,Java and a PostgreSQL-backend

Updated on October 17, 2020

Comments

  • Florian
    Florian over 3 years

    While searching the interweb for a solution for my VB.net problems I often find helpful articles on a specific topic, but the code is C#. That is no big problem but it cost some time to convert it to VB manually. There are some sites that offer code converters from C# to VB and vice versa, but to fix all the flaws after the code-conversion is nearly as time-consuming as doing it by myself in the first place.

    Till now I am using http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

    Do you know something better?

    • kjack
      kjack over 8 years
      Closed so we'll never know if there is a better one than there was in 2008
    • mcfea
      mcfea over 8 years
      ILSpy is working for me in 2016. Pretty much the same thing as the accepted Reflector answer.
    • dotNET
      dotNET over 6 years
      Anyone who lands here, I have been using Econ Netvert for ages and it works smoothly most of the time and is offline. Just make sure you select ICSharp 4.0 from the providers list (top-right). Note that it doesn't handle some of the new language constructs (lambdas, anonymous functions, null-coalescing), but otherwise works fine 95% of the time. Works both ways (C# <-> VB.NET) and can handle entire projects too. Note that the project is no longer being maintained.
  • Daniel
    Daniel almost 15 years
    Unfortunaly, the decompiler to VB in Reflector is quite buggy. Often you'll end up with code that compiles, but produces different results. Try this as a test case to evaluate code converters: double x = 3 / 2; double y = 3.0 / 2; int z = (int)(-3.5); "x" should be 1.0 (many converters produce code that results in x=1.5), y should be 1.5, z should be -3 (many converters produce code that results in z=-4)
  • Daniel
    Daniel almost 15 years
    Note: to see the problems I mentioned in Reflector, you need to use temporary variables, otherwise the C# compiler optimizes the calculation away and the bug in Reflector isn't triggered.
  • Dave Anderson
    Dave Anderson over 12 years
    This has a handy option to upload a zip of source and then download a zip with them all converted and a conversion error report.
  • Fernando Gonzalez Sanchez
    Fernando Gonzalez Sanchez almost 10 years
    Links 1st & 3rd are broken now.
  • Admin
    Admin over 6 years
    It no longer converts comments! No feedback support
  • Darren Kopp
    Darren Kopp over 6 years
    please see my edits @eschneider. maybe the new one will work better for you.
  • Admin
    Admin over 6 years
    Just tried it again, not working at all now. System.NotSupportedException: NotInheritableKeyword not supported!
  • Admin
    Admin over 6 years
    Few other issues I see: vb.net collections .Item(0) fails, .ToString fails "Note missing ()". nameof fails "Case issues, others like this", vb.net arrays vs c# array issues "() vs []" and line spacing is not maintained
  • Admin
    Admin over 6 years
    roslyn version works but still no comments
  • Graham
    Graham about 6 years
    If you put your example into roslyncodeconverter.azurewebsites.net it converts the same as your hand written version (except that you changed the names of things in your handmade conversion)
  • Graham
    Graham about 6 years
    The SharpDevelop converter you mentioned also has a Visual Studio extension which is much more accurate since it uses the project context.