| The Select Case statement enables
you to run a series of statements
based on the value of a single variable
. The Select Case statement identifies
the variable to be evaluated and then
a series of Case statements specifies
the possible values . If the value
of the variable matches the value
indicated in the Case statement the
commands after the Case statement
executes. If the value does not match
the program proceeds to the next Case
statement.
Here is the syntax of the Select
Case block.
Select Case Testvalue
Case value1 :
Statement 1
Case value 2 :
Statement 2
End Select
The first statement is the Select
Case statement , this identifies the
value to be tested against possible
results . This value is represented
by the Testvalue argument can be any
valid numeric or string expression.
Each condition is started by a Case
statement . The Case statement identifies
the expression to which the Testvalue
is compared. The Case statement can
express a single value or a range
of values . If the Testvalue argument
is equal to or within the range of
the expression the commands after
the Case statement are run . The program
runs the commands between the Case
statement and the next Case statement.
If the Testvalue is not in the range
in the Case statement the next Case
statement is evaluated.
Example
Select Case intAnswer
Case 1:
MsgBox"that is the wrong answer"
Case 2:
MsgBox"that is the wrong answer"
Case 3:
MsgBox"Well done that is the
correct answer"
End Select
This is a simple example from a program
that displays a message depending
on the number the user enters.
here is another example which shows
how to check a range , to achieve
this you use the To keyword
Select Case intMyAge
Case 1 To 16
Print " Too young to enter"
Case 17 To 99
Print " Entry accepted"
Case Else
Print " Not in range"
End Select
In this example if the number represented
by intMyAge is in the range 1 to 16
then the message under the line is
executed . Notice the colon (:) is
omitted from the Case and also that
a Case Else is supplied which deals
with values outside the range you
are looking for.
|