如何用Case检测参数

08r*_*tsj 6 .net vb.net select-case

我正在尝试创建一系列可以获取参数的命令.拿起我正在使用Select Case的每个单独的命令这个问题是如果我使用的话,我无法感知'参数'(字符串的第二部分)Case Else.如果我不使用Case Else那么我无法处理不正确的命令并将它们发送到所需的程序.

例如:

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
Run Code Online (Sandbox Code Playgroud)

如果命令是'Brrr​​rr',它会调用Errors().然而,如果命令是"吃食物",它仍会调用Errors()而不是将参数传递给Eat程序.

编辑,因为它现在不起作用. 我已经尝试了所建议的内容,但我仍然有完全相同的问题.它似乎兼而有之Command.StartsWith,Command.Contains因为如果我尝试进入'吃食物',它仍然不会认为它是一个案例.

例:

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
Run Code Online (Sandbox Code Playgroud)

Ash*_*way 5

您需要使用IF语句来执行所需的检查.
你有两个选择StartsWithContains.
StartsWith将允许您检查您的关键字是否在字符串的开头.

  • 吃香蕉=真
  • 香蕉吃=假

Contains 将允许您检查您的关键字是否退出任何地方.

  • 吃香蕉=真
  • 香蕉吃=真

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
Run Code Online (Sandbox Code Playgroud)

包含示例

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
Run Code Online (Sandbox Code Playgroud)

另外,StartsWith或者Contains你可以这样做,StringComparison这将允许你忽略大小写.

If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then
Run Code Online (Sandbox Code Playgroud)

要么

If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then
Run Code Online (Sandbox Code Playgroud)

忽略该案例将匹配"EAT","Eat","eAt"等.


Sar*_*yan 4

确实,您可以保留您的Select Case块,但由于您没有检查是否完全相等,因此String您应该检查Command.Contains("Eat")Command.StartsWith("Eat")取决于您的目标

例如;

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
Run Code Online (Sandbox Code Playgroud)