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#: Invalid call to Conversions.ToInteger for nullable types #865

Closed
Yozer opened this issue Apr 4, 2022 · 1 comment · Fixed by #872
Closed

VB -> C#: Invalid call to Conversions.ToInteger for nullable types #865

Yozer opened this issue Apr 4, 2022 · 1 comment · Fixed by #872
Assignees
Labels
output logic error A bug where the converted output behaves differently to the input code VB -> C# Specific to VB -> C# conversion

Comments

@Yozer
Copy link
Member

Yozer commented Apr 4, 2022

Called Test method with Nothing/null:

VB.Net input code

Enum TestEnum
    None
    All
End Enum
Sub Test(a As Integer?)
    Dim enm = CType(a, TestEnum)
    If enm = TestEnum.None Then Console.WriteLine("Bad")
End Sub

Erroneous output

public static void Test(int? a)
{
    TestEnum enm = (TestEnum)Conversions.ToInteger(a);
    if (enm == TestEnum.None)
        Console.WriteLine("Bad");
}

Conversions.ToInteger(a) will return 0 which corresponds to TestEnum.None.

Expected output

public static void Test(int? a)
{
    TestEnum enm = (TestEnum)a; // or (TestEnum)a.Value;
    if (enm == TestEnum.None)
        Console.WriteLine("Bad");
}

Should throw InvalidOperationException.

Details

  • Product in use: e.g. codeconverter.icsharpcode.net / VS extension / both
  • Version in use: e.g. 5.6.3 or a commit hash a26c6f9
  • Did you see it working in a previous version, which?
  • Any other relevant information to the issue, or your interest in contributing a fix.

Probably all nullables are affected.
Possibly the fix in #862 should include strings and fractionals for Conversion.ToInteger. Need to investigate how weird VB is in this aspect.

@Yozer Yozer added output logic error A bug where the converted output behaves differently to the input code VB -> C# Specific to VB -> C# conversion labels Apr 4, 2022
@Yozer Yozer changed the title VB -> C#: Invalid call to Conversions.ToInteger to nullable types VB -> C#: Invalid call to Conversions.ToInteger for nullable types Apr 4, 2022
@Yozer
Copy link
Member Author

Yozer commented Apr 5, 2022

Or even better. Try to use TypeConversionAnalyzer for CType casts and see what's not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
output logic error A bug where the converted output behaves differently to the input code VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant