Search code examples
.netvb.netselect-case

How to sense parameters with Case


I'm trying to create a series of commands that can take parameters. To pick up each separate command I am using Select Case The problem with this is I can't sense the 'parameters' (the second part of the string) if I use Case Else. If I don't use Case Else then I can't handle incorrect commands and send them off to the required procedure.

For Example:

Private Sub AllocateType(ByVal Command As String)

    Select Case Command
        Case "Eat"
            'Call Eat procedure
        Case "Use"
            'Call Use procedure
        Case "Quit"

        Case "Pause"

        Case "Go"

        Case Else
            Errors() 'Error handling procedure
    End Select

End Sub

If the command was 'Brrrrr', it would call Errors(). Yet if the command was 'Eat Food', it would still call Errors() and not pass the parameters to the Eat procedure.

Edit, because it now doesn't work. I've tried what was suggested, but I still have exactly the same problem. It seems both Command.StartsWith and Command.Contains don't work because if I try to enter 'Eat Food', it still doesn't recognise it as a case.

Example:

Select Case Command
    Case Command.Contains("Eat")
        Output("TESTING")
        If Len(Command) > 4 Then
            Command = Mid(Command, 4, (Len(Command) - 4))
            Interaction(Command)
        Else
            Output("Eat What?")
        End If
    Case "Eat"
        Output("Eat What?")
    Case Command.StartsWith("Use")
        If Len(Command) > 4 Then
            Command = Mid(Command, 4, (Len(Command) - 4))
            Interaction(Command)
        Else
            Output("Use What?")
        End If
    Case "Use"
        Output("Use What?")
        'Case Else
        '    Errors()
End Select

Solution

  • True, you can keep your Select Case block, but since you are not checking for exact equality of String you should check for Command.Contains("Eat") or Command.StartsWith("Eat") depending on your goal

    for insance;

    Private Sub AllocateType(ByVal Command As String)
    
        Select Case True
            Case Command.StartsWith("Eat")
                'Call Eat procedure
            Case Command.StartsWith("Use")
                'Call Use procedure
            Case Command.StartsWith("Quit")
            Case Command.StartsWith("Pause")
            Case Command.StartsWith("Go")
            Case Else
                Errors() 'Error handling procedure
        End Select
    
    End Sub