How to sense parameters with Case
Asked Answered
D

3

6

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
Duhamel answered 5/1, 2014 at 10:10 Comment(5)
I have updated my answer with full examples for you :)Idiom
The edit explains it. Command.StartsWith doesn't work.Duhamel
That's weird. Just tried it with If and ElseIf instead of case and it works perfectly.Duhamel
I do apologise. I am new to the conventions of Stack Overflow. Anyway, All is working now.Duhamel
No need to, welcome among and glad it helpedWorsted
U
4

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
Unmentionable answered 5/1, 2014 at 10:16 Comment(0)
I
5

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.

Idiom answered 5/1, 2014 at 10:15 Comment(0)
U
4

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
Unmentionable answered 5/1, 2014 at 10:16 Comment(0)
M
4

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
Moller answered 5/1, 2014 at 10:34 Comment(1)
+1 i just remembered you are right, you can evaluate a value inside case statement in VB.NET which is not like C# where a constant value is expected in case statement. Been ages since i coded VB.NET (:Worsted

© 2022 - 2024 — McMap. All rights reserved.