Raj Aththanayake's Blog Raj Aththanayake's Blog | Tips for converting VB.NET to C#
Home > C#

Tips for converting VB.NET to C#

29. May 2010

As soon as anyone sees this title, most of you would think, ‘Ah..that’s easy, use a tool and it will do the job for you’. Recently I have been given a small task to convert some VB.NET code to C#. I’m not good at writing VB.NET so this was a challenge. 

 

As any other C# developer, I decided to use a conversion tool. I copied and paste the VB.NET code I needed to translate into C#.NET. Another approach would be to use the Refactor. If you can include this function in an assembly you can easily view the C# equivalent of the code using Refactor. I got it translated using a conversion tool. However I saw the translated code was bit confusing. Which also bring me to the next topic that you should not blindly translate your VB.NET code to C#. 

 

Take a look at this example.

VB.NET
Public Function GetAppGUID(ByVal sectionId As String) As String 
 
    Dim hexString As String = Nothing 
    Dim i As Integer 
    Dim guidlen As Integer 
 
    guidlen = 16 
 
    If sectionId.Length < guidlen Then 
        sectionId = sectionId & New String(" ".Chars(0), guidlen - sectionId.Length) 
    End If 
 
    For i = 1 To guidlen 
        hexString = hexString & Hex(Asc(Mid(sectionId, i, 1))) 
    Next 
 
    GetAppGUID = hexString 
 
End Function
C#
public string GetAppGUID(string sectionId) 
{
          
	    
	string hexString = null; 
	int i = 0; 
	int guidlen = 0; 
	    
	guidlen = 16; 
	    
	if (sectionId.Length < guidlen) { 
	        sectionId = sectionId + new string(" ".Chars(0), guidlen - sectionId.Length); 
	} 
	    
	for (i = 1; i <= guidlen; i++) { 
	     hexString = hexString + Conversion.Hex(Strings.Asc(Strings.Mid(sectionId, i, 1))); 
	} 
	    
	        
	return hexString; 
}

As you can see the converted function require Microsoft.VisualBasic.dll for Conversion and Asc functions. In some cases yes you would use this dll but most of the time you don’t have to. You can easily write this function in C# using same line of code but not using Microsoft.VisualBasic.dll

private string GetAppGUID(string sectionId) 
    { 
        string hexString = null; 
        int i = 0; 
        int guidLength = 0; 
 
        guidLength = 16; 
 
        if (sectionId.Length < guidLength) 
        { 
            sectionId = sectionId + new string(" "[0], guidLength - sectionId.Length); 
        } 
 
        foreach (char c in sectionId) 
        { 
            int tmp = c; 
            hexString += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())) 
        } 
 
        return hexString; 
    }  

So this is my opinion.

a. If you ever have to convert VB.NET to C#, you may use a conversion tool or Reflector but make sure you are not blindly converting the code.

b. If the converted code require Microsoft.VisualBasic.dll, then try to avoid this dll. Refactor your code to use the equivalent C# syntax. To include a completely different dll in your C# project for just a small method/syntax change is not really worth it.

c. Don’t take your task as “hell this is not the language I’m familiar with”. See this opportunity is an excellent way to learn another language. It is quite common in the industry that you get to do these types of tasks often. Which also prove you are an all-rounded developer,

C#

Comments

5/29/2010 8:49:37 AM #
Great info. Never thought you could use Reflector for this. I like your tips. Thanks alot for the post.
Abdul
Abdul
5/29/2010 5:05:45 PM #
Hi
I'm a VB.NET developer. Your points can also apply when converting from VB.NET to C# I guess. Thanks appreciated.
Sam
Sam
5/30/2010 5:23:03 AM #
Thanks for the tips. It is really useful. I do these type of tasks all the time but never knew that I could use Reflector.
5/30/2010 8:33:53 AM #
I am gald that you have shared this programming on your blog.I was looking for it.I hope you will post more informative blogs.
Alan
Alan
6/8/2010 7:35:25 AM #
Nice post. Thanks.
6/18/2010 2:01:49 AM #
My friend suggested me to visit your blog. Very well explained. I would like to say that it is very interesting to read your blog.
7/23/2010 8:51:20 AM #
Hi there, I just wanted to say thanks for this informative post, can you please allow me to post it on my blog?
Comments are closed