Вопрос

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
Это было полезно?

Решение 2

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

Другие советы

You need to use an IF statement to preform the kind of check you want.
You have two options StartsWith and Contains.
StartsWith will allow you to check if your keywords are at the start of the string.

  • Eat Banana = True
  • Banana Eat = False

Contains will allow you to check if your keyword exits anywhere.

  • Eat Banana = True
  • Banana Eat = True

StartsWith Example

Private Sub AllocateType(ByVal Command As String)

    If Command.StartsWith("Eat") Then
        'Call Eat procedure
    Else If Command.StartsWith("Use") Then
        'Call Use procedure
    Else If Command.StartsWith("Quit") Then
        'Call Quit procedure
    Else If Command.StartsWith("Pause") Then
        'Call Pause procedure
    Else If Command.StartsWith("Go") Then
        'Call Go procedure
    Else
        Errors()
    End If

End Sub

Contains Example

Private Sub AllocateType(ByVal Command As String)

    If Command.Contains("Eat") Then
        'Call Eat procedure
    Else If Command.Contains("Use") Then
        'Call Use procedure
    Else If Command.Contains("Quit") Then
        'Call Quit procedure
    Else If Command.Contains("Pause") Then
        'Call Pause procedure
    Else If Command.Contains("Go") Then
        'Call Go procedure
    Else
        Errors()
    End If

End Sub

Also with StartsWith or Contains you can do StringComparison this will allow you to ignore case.

If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then

OR

If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then

Ignoring the case will match "EAT", "Eat", "eAt", etc.

You can keep Case structure:

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top