views:

2773

answers:

11

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?

+7  A: 

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.

Hallgrim
Nice hint Hallgrim. I will try that :)
Florian
Yeah +1 here, Reflect the compiled code into your language
Slace
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
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.
Daniel
+2  A: 

Telerik makes a great converter. http://converter.telerik.com/

Darren Kopp
+2  A: 

Last I checked, SharpDevelop has one and it is open source too.

Geoffrey Chetwood
+4  A: 

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 )

Ricky AH
A: 

Carlos Aguilar Mares has had an online converter for about 40 forevers - Code Translator but I would agree that Reflector is the better answer.

MotoWilliams
+1  A: 

While not answering your question, I will say that I have been in a similar position.

I realised that code samples in C# were awkward when I was really starting out in .NET, but a few weeks into my first project (after I grown more familiar with the .NET framework and VB.NET itself), I found that it was interesting and sometimes beneficial to have to reverse-engineer the C# code. Not just in terms of syntax, but also learning about the subtle differences in approach - it's useful to be open-minded in this respect.

I'm sticking with VB.NET as I learn more and more about the framework, but before long I'll dip my to into C# with the intention of becoming 'multi-lingual'.

CJM
+2  A: 

The converter at Developer Fusion (http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx) has always worked great for me.

Matt Hanson
A: 

Currently I use a plugin for VS2005 that I found on CodeProject (http://www.codeproject.com/KB/cs/Code_convert_add-in.aspx); it use an external service (http://www.carlosag.net/Tools/CodeTranslator/) to perform translation.

Occasionally, when I'm offline, I use a converter tool (http://www.kamalpatel.net/ConvertCSharp2VB.aspx).

Davide
+4  A: 

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

I couldn't agree more. +1 from me
Vnuk
A: 

The one at http://www.developerfusion.com/tools/convert/csharp-to-vb/ (new url) now supports .NET 3.5 syntax (thanks to the #develop guys once again), and will automatically copy the results to your clipboard :)

+2  A: 

You can load your DLL or EXE into Redgate's (formerly Lutz Roeder's) .Net Reflector, select your method and then the desired language from the language combo. The code of the selected method will be displayed in the selected language.

I hope this helps.

Germstorm