Tips for converting VB.NET to C#

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,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s