CodeConverter
CodeConverter copied to clipboard
VB -> C#: Pulling variables before the loop can rename variables in an unexpected way.
VB.Net input code
Private Sub TestMethod()
For i = 1 To 2
If i = 1 Then
Dim a As Integer
a += 1
System.Console.WriteLine(a)
End If
Next
End Sub
Erroneous output
private void TestMethod()
{
var a = default(int);
for (int i = 1; i <= 2; i++)
{
if (i == 1)
{
ph38013f178570470fb7aea44e2e2f4f41 += 1;
System.Console.WriteLine(ph38013f178570470fb7aea44e2e2f4f41);
}
}
}
Expected output
private void TestMethod()
{
var a = default(int);
for (int i = 1; i <= 2; i++)
{
if (i == 1)
{
a+= 1;
System.Console.WriteLine(a);
}
}
}
Details
- Product in use: e.g. codeconverter.icsharpcode.net / VS extension / both
- Version in use: e.g. 5.6.3 or a commit hash (if it's a 3rd party tool using this library, try one of the above) https://github.com/icsharpcode/CodeConverter/commit/dce1ab91d43e437e83e8127267436ae7bfb0bb9e
- Did you see it working in a previous version, which?
- Any other relevant information to the issue, or your interest in contributing a fix.
I can try to take a look, probably the issue is related to nested scope that is not a loop.
Another problem. Probably a bit more serious. My guess is that we throw duplicated key exceptions from the dictionary that handles renaming.
Private Sub TestMethod2()
For i = 1 To 2
If i = 1 Then
Dim a As Integer
a += 1
System.Console.WriteLine(a)
Else If i = 2 Then
Dim a As Integer
a += 1
System.Console.WriteLine(a)
End If
Next
End Sub
private void TestMethod2()
{
var a = default(int);
for (int i = 1; i <= 2; i++)
;
}
It's a bit surprising to me that we don't create here some kind of #error directive.
And third issue. Still related to renaming so I will keep it under this issue.
Public Class Data
Public Property a as Integer
End Class
Public Class VisualBasicClass
Private Sub TestMethod3()
For j= 1 To 2
For i = 1 To 2
Dim a As Integer
If i = 1 Then
Dim d as New Data
a += 1
System.Console.WriteLine(a)
System.Console.WriteLine(d.a)
End If
Next
Next
End Sub
End Class
var a1 = default(int);
for (int j = 1; j <= 2; j++)
{
for (int i = 1; i <= 2; i++)
{
if (i == 1)
{
var d = new Data();
a1 += 1;
System.Console.WriteLine(a1);
System.Console.WriteLine(d.a1);
}
}
}
- We renamed a variable unrelated to the variable being pulled before the loop. Current renaming logic makes a text comparison on Identifiers
- Not really an issue but we didn't have to do renaming here as there is no conflicting variable.