icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
795 stars 207 forks source link

VB -> C#: Case `Empty` is not translated to `string.IsNullOrEmpty()` #1104

Closed Saibamen closed 2 weeks ago

Saibamen commented 1 month ago

VB.Net input code

Dim Sprossenart As String
Dim Hoehe As String

Select Case Sprossenart
    Case Empty, "ohne"
        Hoehe = 0
End Select

Erroneous output

var Sprossenart = default(string);
string Hoehe;

switch (Sprossenart ?? "")
{
    case var @case when @case == Empty: // <---- CS0103: The name 'Empty' does not exist in the current context
    case "ohne":
        {
            Hoehe = 0.ToString();
            break;
        }
}

Expected output

var Sprossenart = default(string);
string Hoehe;

switch (Sprossenart ?? "")
{
    case var @case when string.IsNullOrEmpty(@case):
    case "ohne":
    {
        Hoehe = 0.ToString();
        break;
    }
}

Details

GrahamTheCoder commented 1 month ago

If you ctrl click on Emptyin the vb net example within visual studio, what does it resolve to there? Is it string.Empty and is there a corresponding import?

Saibamen commented 3 weeks ago

I'm using VB6 SP6

image

GrahamTheCoder commented 3 weeks ago

If you upgrade to vb.net first, does it work from the upgraded result? If so, the project likely defines a global import for string. Without knowing that (I. E if you just paste the contents of the file into the Web converter), the converter can't guess which type is referred to.

Saibamen commented 3 weeks ago

Visual Basic 2008 Express Edition upgraded the code to this:

Option Strict Off
Option Explicit On
Module SubMainModule

    Public Sub Main()
        Dim Sprossenart As String
        Dim Hoehe As String

        Select Case Sprossenart
            Case CStr(Nothing), "ohne"
                Hoehe = CStr(0)
        End Select
    End Sub
End Module

image

Saibamen commented 2 weeks ago

ping @GrahamTheCoder

GrahamTheCoder commented 2 weeks ago

I ran the code you just gave through the we converter. It compiles and runs the same: https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwLACgCWVgFMxQCGANgAQACAjAGykAOhYwmJFATKQMoQBGAsoWx8A9nAjFcGAN4YMpeezZyFM9AvWkAboy50wwgM4HcRJqQC8pOLgBmhccAAUVAAwBKANzKNFSi9IAEsK4ABa4XujeGgYA7pjAAMYhpI6ceobGpsCkAPw5pABEBW5R6qo+PgmExlo65ORVNTFhUBQN1bgWlkUgpRWNnQXCIVC4Bb1qFVPlU7PyQaGdli4AdAAqwpzAYNgA5o6efXPqPGC4hADWEccaAL5HpPeT8k+3QA===

Is there an issue here I can't see?

Saibamen commented 2 weeks ago

If this works for VB.NET -> C#, this is ok