CSharpToVB icon indicating copy to clipboard operation
CSharpToVB copied to clipboard

Name clashes

Open GrahamTheCoder opened this issue 4 years ago • 1 comments

Input

public class ClashingNames
{
    private string ab;

    void Test()
    {
        object aB = 5;
        int Ab = 7;
    }

    void Test2(int ab)
    {
        object aB = 5;
        int AB =  6;
        string s = nameof(AB);
        string s2 = nameof(ab);
    }
}

Expected output (from https://codeconverter.icsharpcode.net/)

Public Class x
    Private ab As String

    Private Sub Test()
        Dim lAB As Object = 5
        Dim Ab As Integer = 7
    End Sub

    Private Sub Test2(ByVal ab As Integer)
        Dim lAB As Object = 5
        Dim lAB1 As Integer = 6
        Dim s As String = NameOf(lAB1)
        Dim s2 As String = NameOf(ab)
    End Sub
End Class

Actual output

Option Compare Text
Option Explicit On
Option Infer Off
Option Strict On


Public Class x
    Private _ab As String

    Private Sub Test()
        Dim aB As Object = 5
        Dim Ab As Integer = 7
    End Sub

    Private Sub Test2(_ab As Integer)
        Dim aB As Object = 5
        Dim AB As Integer = 6
        Dim s As String = NameOf(AB)
        Dim s2 As String = NameOf(_ab)
    End Sub


End Class

Compilation errors

error BC30288: Local variable 'Ab' is already declared in the current block.
error BC30288: Local variable 'AB' is already declared in the current block.
  • Note: Obviously this is an extreme example. I did see the existing renaming code add "_Renamed" for one variable in another case I tried, so it does sometimes work.

Possible solution

After you forked from https://github.com/icsharpcode/CodeConverter/ I implemented a very robust and isolated way of renaming to avoid this issue. All you need to do is call this method on the project object just before you convert: https://github.com/icsharpcode/CodeConverter/blob/master/CodeConverter/VB/ClashingMemberRenamer.cs#L21

It's not currently a public class, but I could make it public if you want to just use it via nuget rather than copying/converting?

GrahamTheCoder avatar Aug 09 '20 13:08 GrahamTheCoder

@GrahamTheCoder I fixed in converter 3.4.6.2. There may still be issues in full project conversion. I added solution converter which works very well because I pick up all the Types and I now convert gRPC projects which require parts in to stay in C# but be referenced by VB. I also do Async ForEach and many Patterns I have a solution for Ref Returns but not sure how to do an automatic conversion that doesn't break ByRef in general. Biggest issue in any kind of Merge is mine in in VB and yours is in C#.

paul1956 avatar Aug 10 '20 02:08 paul1956