What is the best C# to VB.net converter?
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://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).
Related videos on Youtube
Comments
-
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 over 8 yearsClosed so we'll never know if there is a better one than there was in 2008
-
mcfea over 8 yearsILSpy is working for me in 2016. Pretty much the same thing as the accepted Reflector answer.
-
dotNET over 6 yearsAnyone 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 almost 15 yearsUnfortunaly, 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 almost 15 yearsNote: 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 over 12 yearsThis 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 almost 10 yearsLinks 1st & 3rd are broken now.
-
Admin over 6 yearsIt no longer converts comments! No feedback support
-
Darren Kopp over 6 yearsplease see my edits @eschneider. maybe the new one will work better for you.
-
Admin over 6 yearsJust tried it again, not working at all now. System.NotSupportedException: NotInheritableKeyword not supported!
-
Admin over 6 yearsFew 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 over 6 yearsroslyn version works but still no comments
-
Graham about 6 yearsIf 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 about 6 yearsThe SharpDevelop converter you mentioned also has a Visual Studio extension which is much more accurate since it uses the project context.