KnowDotNet NetRefactor

Convert VB.NET to C# using Macros and Regular Expressions

Realize the Power of Macros

by Les Smith
Print this Article Discuss in Forums

Macros and Regular Expressions can make converting VB.NET to C# easier.  I have not purchased a VB.NET to C# Converter, so I am improvising in converting to C#.

First, I found a link that does a decent job of converting from VB.NET to C#, but it has some fairly decent (documented) shortcomings.  But, I am still thankful for the tool, since it's free, and I have not found this type of converter to be foolproof.  The link for this tool is
convert VB.NET to C#.

Many of the short comings can be fixed with a simple combination of Macros using Regular Expressions.  In all cases described below, you will need to place the macros in a module of the Macro IDE.  From there, you can double-click on the desired macro to have it execute on the highlighted code in the Visual Studio IDE.
One of the worst shortcomings and the hardest to convert is the use of () in VB.NET for function calls, array declarations and references, and in a database driven application, datarow enumerators.  The following line of code is most annoying.

    Subject = System.Convert.ToString(dt.Rows(0].Item("patient_ssn"));

It can be corrected by highlighting the following
(0].Item("patient_ssn") in the source line and double-clicking the CnvtBraceSet macro in the macro explorer.

    Sub CnvtBraceSet()
        
Dim ts As TextSelection = DTE.ActiveWindow.Selection
        
Dim code As String = ts.Text
        
If code.Length = 0 Then Exit Sub
        code = Regex.Replace(code, _
          "^(?<open1>\()(?<data>.*?)(?<close1>\)).Item(?<open2>\()(?<enumer>.*)(?<close>\))", _
          "[${data}][${enumer}]")
        ts.Text = code
    
End Sub


The resulting line will be:

    Subject = System.Convert.ToString(dt.Rows(0].Item("patient_ssn"));


Sometimes, you will not have both pieces of the code line shown above.  For example, you may only have the following line of code to work with.

    dr = dt.Rows(0);

This macro will correct that problem.  Highlight the (0) and double-click the CnvtParansToBrackets macro in the Macro Explorer.

    Sub CnvtParansToBrackets()
        
Dim ts As TextSelection = DTE.ActiveWindow.Selection
        
Dim code As String = ts.Text
        
If code.Length = 0 Then Exit Sub
        code = Regex.Replace(code, "^(?<open>\()(?<enumer>.*)(?<close>\))", "[${enumer}]")
        ts.Text = code
    
End Sub

And the resulting line will be;

   dr = dt.Rows[0];


Another problem is failue to change
Me. to this., as in the following line of code.

    Me.txtSubject.Text = s;

This macro will solve that problem.  Place it in the Macro IDE Module.  Next, simply double click
Me and then double click the macro CnvtMeToThis in the Macro Explorer.

    Sub CnvtMeToThis()
        
Dim ts As TextSelection = DTE.ActiveWindow.Selection
        
Dim code As String = ts.Text
        
If code.Length = 0 Then Exit Sub
        code = code.Replace("Me.", "this.")
        code = code.Replace(vbCrLf, ";" & vbCrLf)
        ts.Text = code
    
End Sub

A final problem, that I will address, is that the converter does not add "()" to String methods as in the following line of code.

    s = sLine.Substring(0, iPtr) + " " + sLine.Substring(iPtr + 1).Trim;

This macro will solve that problem.

    
Sub AddParans()
        
Dim ts As TextSelection = DTE.ActiveWindow.Selection
        
Dim code As String = ts.Text
        
If code.Length = 0 Then Exit Sub
        ts.Text = code & "()"
    
End Sub

Obviously, this is not an exhausting way to solve formatting problems, but it should get you started.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing