Monday, November 2, 2009

Tools for Converting C# code to VB.NET (or vice versa) and a little => ... Function(), Lambdas too

This seems to be common knowledge for the majority of .NET developers that need help converting C# code to VB.NET or the other way around, but from time to time I still see a new developer to the community that does not know of the avaliable tools to help with this. I also have seen that even some of the more seasoned developers are unaware that there is more than (1) site avaliable to help with this conversion process. So without further ado, here are (3) sites with brief descriptions that will help users conver C# to VB.NET code:

  1. developerFusion's Convert C# to VB.NET - This is probably the most well know site and referenced most often. I typically start with this one: http://www.developerfusion.com/tools/convert/csharp-to-vb/
  2. Code Converter Provided by Telerik - This is another great and stable converter online. I typically go to this one if the code to convert is complex or there were any issues with the converter: http://converter.telerik.com/
  3. KamalPatel.Net - Convert C# to VB .NET - Several years ago I used this one as the defacto converter, but somewhere along the lines the code I was converting was getting too complex, or the site was not upkept anymore because it shows issues converting often. I now come here lastly: http://www.kamalpatel.net/ConvertCSharp2VB.aspx

Now, one of the issues all the converters seem to have issue with is .NET Lambda Expressions. Recently, I have found several powerful code snippets in C# including Labmda expressions that none of the converters would convert properly. This code will need a little extra help in getting it converted. Below I have a brief example of a C# Labmda expression and the equivelent VB.NET code that I had to modify. Hopefully, this code will help guide readers to get started with a proper conversion of Lambda expressions.


The C# version:


.Aggregate(new StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString());

The VB.NET version:


.Aggregate(New StringBuilder(), Function(sb, node) sb.Append(node.ToString()), Function(sb) sb.ToString())

Both code examples take use of passing values to a function to shorthand the return, however in VB.NET the syntax is more explicit with the 'Function' statement being required. A good reference for Lambda expressions for each language can be found below.


Lambda Expressions (VB.NET):
http://msdn.microsoft.com/en-us/library/bb531253.aspx

Lambda Expressions (C#):
http://msdn.microsoft.com/en-us/library/bb397687.aspx

This was by no means a full entry on .NET Lambda expressions, but I just wanted to briefly highlight some of the differences syntactically between the (2) languages, and where the converters may have issue with more complex code.

3 comments:

  1. Hi,
    I need to Convert the VB code to c# logic using asp.net,c#. Can you give me an some idea's.I dont want any Tool's.(I am expecting Logic)

    Thanks,
    Maniraj

    ReplyDelete