Page 1 of 1

Tutorial for the Select Case True Command

PostPosted: Mon Jun 22, 2009 8:28 pm
by cpkspikyhair
Select Case True Tutorial

The Select Case command in VBScript is usually used to test the value of one variable in a neat, list-like way.

For example:

Code: Select all
Select Case var
     Case 1
           MsgBox "Hello."
     Case 2
           MsgBox "Goodbye."
End Select


It works a lot like the If....ElseIf statements, but it is neater and once you get used to it, it can be easier to debug.



But many people don't know about another way to use Select Case, the Select Case True command.

The Select Case True command tests multiple statements, or "cases", to see if they are true, instead of testing one variable's value. It works in a way that is almost backwards from the original Select Case. It can also test multiple statements at the same time, using the conjunction "And".

For example, this shows a way you can test multiple variables' values:

Code: Select all
Select Case True
     Case (upvar = 1)
           MsgBox "You hit the UP key."
     Case (leftvar = 1)
           MsgBox "You hit the LEFT key."
     Case (downvar = 1)
           MsgBox "You hit the DOWN key."
     Case (rightvar = 1)
           MsgBox "You hit the RIGHT key."
     Case ((upvar = 0) And (leftvar = 0) And (downvar = 0) And (rightvar = 0))
           MsgBox "You did not hit any key."
End Select


Again, it does work very much like using If...ElseIf, but it is neater-looking and more organized, and in my opinion it is easier to keep track of multiple Select Case True statements than a hierarchy of If and ElseIf statements.

PostPosted: Mon Jun 22, 2009 11:44 pm
by Lyberodoggy
Thanks for this one :)

PostPosted: Wed Jun 24, 2009 4:39 pm
by reneuend
Cool Idea, Spike! I hadn't thought of this! :)