Skip to content

C# -> VB: Switch Expression in Return Statement Converts to an Empty String #983

Open

Description

@RussellSmith2

Input code

private static string GetBaseForNewName(ISymbol declaration)
{
  string name = GetName(declaration);
  return declaration.Kind switch {
      SymbolKind.Method => name + "Method",
      SymbolKind.Property => name + "Prop",
      SymbolKind.NamedType => name + "Type",
      SymbolKind.Field => name + "Field",
      _ =>  declaration.Kind.ToString().ToLowerInvariant()[0] + name.Substring(0, 1).ToUpperInvariant() + name.Substring(1)
  };
}

Erroneous output

Private Shared Function GetBaseForNewName(ByVal declaration As ISymbol) As String
    Dim name As String = GetName(declaration)
    Return
End Function

Expected output

Private Shared Function GetBaseForNewName(declaration As ISymbol) As String
    Dim name = GetName(declaration)
    Select Case declaration.Kind
        Case SymbolKind.Method
          Return name & "Method"
        Case SymbolKind.Property
          Return name & "Prop"
        Case SymbolKind.NamedType
          Return name & "Type"
        Case SymbolKind.Field
          Return name & "Field"
        Case Else
          Return declaration.Kind.ToString().ToLowerInvariant()(0) & name.Substring(0, 1).ToUpperInvariant() & name.Substring(1)
    End Select
End Function

Details

  • Using build of current repository

Activity

  1. GrahamTheCoder commented on Feb 5, 2023

    @GrahamTheCoder
    Member

    Just a word of caution to anyone trying to implement this: the given example is a special case in two ways:

    • It's within a method
    • The containing statement (return) can simply be repeated within each case label

    I expect this is quite a common case so may be worth focusing on that first and just throwing for other cases, which might need extra variables/functions pulling out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C# -> VBSpecific to C# -> VB conversion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions