Reputation: 880
I have the following select case where I want to do some checks on what a string contains and call some functions for each case. But it seems that if multiple conditions are true it considers only the first condition. The problem is I have about 113 different cases.
Do I have to use an if statement for each case?
Select Case True
Case command.ToUpper.Contains(" S:")
'Do something
Case command.ToUpper.Contains(" C:")
'Do something
Case command.ToUpper.Contains(" *S")
'Do something
Case command.ToUpper.Contains(" *C")
'Do something
Case command.ToUpper.Contains(" CC")
'Do something
Case command.ToUpper.Contains(" SS")
End Select
Upvotes: 1
Views: 1512
Reputation: 61
Just a thought, but what about
dim tempCommand as string = command.ToUpper()
dim match as boolean = true
while match andalso tempCommand.length > 0
select case true
Case tempCommand.Contains(" S:")
'Do something
'then do this
tempCommand = tempCommand.replace(" S:","")
Case tempCommand.Contains(" C:")
'Do something
'then do this
tempCommand = tempCommand.replace(" C:","")
Case tempCommand.Contains(" *S")
'Do something
'then do this
tempCommand = tempCommand.replace(" *S","")
Case tempCommand.Contains(" *C")
'Do something
'then do this
tempCommand = tempCommand.replace(" *C","")
Case tempCommand.Contains(" CC")
'Do something
'then do this
tempCommand = tempCommand.replace(" CC","")
Case command.ToUpper.Contains(" SS")
'then do this
tempCommand = tempCommand.replace(" SS","")
case else match = false
end select
end while
I'm assuming that based on the various criteria you are performing different actions, so I'm not sure how you would do the "perform pattern.action" unless that is a way of doing code dynamically that I don't know about and would be totally cool if it can be done.
My suggestion does destroy the command, which is why I'm using a temporary holding variable so that the original command isn't lost, in case you need it further in the code.
Upvotes: 0
Reputation: 4231
That's how select case is defined. Using a sequence of If statements would work.
Consider a table-driven solution (Pseudocode):
For Each pat In patterns
If command contains pattern.pat
perform pattern.action
Upvotes: 4