Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: String comparison behaviour does not match VB #308

Closed
mrmonday opened this issue May 3, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@mrmonday
Copy link
Contributor

commented May 3, 2019

Input code

Class Class1
    Sub Main()
        Dim s1 As String = Nothing
        Dim s2 As String = ""
        If s1 <> s2 Then
            Throw New Exception()
        End If
    End Sub
End Class

Erroneous output

public class Class1
{
    public static void Main()
    {
        string s1 = null;
        string s2 = "";
        if (s1 != s2)
            throw new Exception();
    }
}

Expected output

using Microsoft.VisualBasic.CompilerServices;

public class Class1
{
    public static void Main()
    {
        string s1 = null;
        string s2 = "";
        if (Operators.CompareString(s1, s2, TextCompare: false) != 0)
            throw new Exception();
    }
}

Details

In VB, the code does not throw. In the current C#, it does throw.

In VB, null and string.Empty compare as the same.

The value of the TextCompare parameter depends on whether Option Compare Text or Option Compare Binary is set (I believe it defaults to Binary).

Unless we're dealing with literals, I don't think there's any way to automatically clean this up.

Source for CompareString: https://github.com/dotnet/corefx/blob/90dc9459b8eaf0c8ac93440e58c7535c67dacc22/src/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Operators.vb#L845-L883

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue May 3, 2019

@mrmonday mrmonday referenced this issue May 3, 2019

Merged

Implement correct string comparisons for VB #309

2 of 2 tasks complete
@GrahamTheCoder

This comment has been minimized.

Copy link
Member

commented May 5, 2019

More specific case of #105 . In this case I'll close as duplicate.

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue May 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.