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")); |
| 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 |
| Subject = System.Convert.ToString(dt.Rows(0].Item("patient_ssn")); |
| dr = dt.Rows(0); |
| 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 |
| dr = dt.Rows[0]; |
| Me.txtSubject.Text = s; |
| 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 |
| s = sLine.Substring(0, iPtr) + " " + sLine.Substring(iPtr + 1).Trim; |
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 |