CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: wrong result for empty char comparisation

Open KunzeAndreas opened this issue 3 years ago • 3 comments

VB.Net input code

Dim testChar As Char = Nothing
Dim testResult = testChar = ""
Assert.That(testResult, "comparing null character with empty string should be true")

Erroneous output

bool testResult = Conversions.ToString(testChar) == "";    

Expected output

bool testResult = testChar == char.MinValue;

Details

  • Product in use: VS extension
  • Version in use: 9.0.0.0
  • Did you see it working in a previous version, which? no

KunzeAndreas avatar May 12 '22 05:05 KunzeAndreas

@KunzeAndreas are you sure that testResult should be true? Running the input code on my machine results in testResult being False: https://ideone.com/usf5f6

SharpLab.io shows that there is actually a call to Conversions.ToString generated by vb compiler.

Something like that is also converted correctly:

Dim testChar As Char = Nothing
Dim testResult = testChar = Microsoft.VisualBasic.ControlChars.NullChar
char testChar = default;
bool testResult = testChar == ControlChars.NullChar;

Am I missing something?

Yozer avatar May 12 '22 11:05 Yozer

@Yozer Yes, I am sure. The test ist running in Visual Studio. Maybe there is a difference between .Net and mono?

image

in dotPeek I see this

private static void CharTest() { bool condition = Operators.CompareString(Conversions.ToString(char.MinValue), "", true) == 0; Debug.Print(condition.ToString()); NUnit.Framework.Assert.That(condition, "comparing null character with empty string should be true"); }

KunzeAndreas avatar May 12 '22 18:05 KunzeAndreas

Thanks @KunzeAndreas, now I see what's happening and how to reproduce it. You need to set:

Option Compare Text

Which then will yield: bool flag = Operators.CompareString(Conversions.ToString(c), "", true) == 0;

and this returns True

Yozer avatar May 13 '22 08:05 Yozer