VB.NET to C# conversion

I recently converted some components on a project from VB.NET to C#, mainly for overloading and better tool support (such as ReSharper). Some of the existing code was generated from my own CodeSmith templates, so a small rewrite to generate C# handled most of that.

VB.NET to C# Converter 1.31

The remaining code, while not extensive in size, is a rather complex affair and the prospect of debugging this code when hand-converted was a little daunting so I decided to give the demo version of VBConversion’s VB.NET to C# Converter 1.31 a spin.

I was rather disappointed it made some rather obvious and stupid mistakes especially when it had converted everything else so well – so much for the 99% accuracy claim. I thought it might just be our project be we adhere to many of Microsoft’s guidelines

Problem 1: Ignores all instance/class variables default assignments and constructors.

Public Class Mine
  Public myObj As MyClass = New MyClass()
  Private myVar As MyEnumeration = MyEnumeration.MyDefault
End Class

Suddenly becomes;

public class Mine {
  public MyClass myObj;
  private MyEnumeration myVar;
}

While the object not being created will soon throw an exception, the defaults for value types is a little harder to track down.

Problem 2: Gives up on Select Case statements after the first case – commenting out the others.

In at least one case it got so confused it commented out substantially more code after the case statement too.

Problem 3: Declares additional unnecessary name-spaces including the one for Microsoft.VisualBasic, despite not needing it.

Either we hadn’t used the CDate/CInt/CLng functions or it had converted them)…

Problem 4: All with statements are converted to be variable with1, even when the old with clause was a simple case.

For example:

With myObj
  .doThis()
End With

becomes;

MyClass with1 = myObj;
with1.doThis();

Using both VB.NET and C# together

If you are going to use both in a project you can bring the syntax a little closer in format, here’s a few tips.

  • Don’t use the Visual Basic CInt/CDate/IsNumber etc functions. Use the .NET Framework equivalents such as Int.Parse, Date.Parse etc. which will work in both languages and are normally faster than these legacy functions.
  • Bring the source closer together by;
    • Putting brackets round if conditions in VB.NET
    • Putting quotes round region declarations in C#
    • Putting attributes on separate lines in VB.NET with a _ suffix
    • Dropping the implementation and interface declarations onto a new line in C#
    • Dropping the base(whatever) onto a new line in C# constructors (good idea anyway)
  • Check out the “Differences Between Visual Basic .NET and Visual C# .NET” white paper on MSDN
  • Use VBCommenter to get C# style XML comment documentation.
  • Use Monitor.Enter() instead of VB.NET’s synclock and C#’s lock.
  • Be aware of the differences regarding math! VB.NET defaults to floating point precision and rounding when converting with CInt/CLng etc.. C# normally uses integer division and casting truncates.

[)amien

No responses (yet!)