Home > Articles

  • Print
  • + Share This

Select Case Statements

The Select Case statement allows you to itemize blocks of code to execute according to a test variable, basically helping you to test against many conditions very efficiently. The following code in Listing 3.9 shows you how to use a Select Case statement, and Figure 3.7 shows the output of the code.

Listing 3.9: Using Select Case Statements (03asp09.htm)


  Sub button1_OnClick()
    Dim TestVar
    Dim varTrait
    'Get the stooge that the user entered
    TestVar = text1.value
    'You start a Select Case statement with
    'the keyword Select Case and the given test
    'an arbitrary variable name.
    Select Case TestVar
      'Then use the keyword Case followed by
      'the value of the test variable to which
      'you want to respond. In this case, if
      'the test variable = "Moe", then assign the
      'value "Bossy" to another variable.
      Case "Moe":
        varTrait = "Bossy"
      'If the test variable = "Larry" assign the
      'string, "Whiney" to the variable, varTrait.
      Case "Larry":
        varTrait = "Whiney"
      'If the test variable = "Curly" assign the
      'string, "Funny" to the variable, varTrait.
      Case "Curly":
        varTrait = "Funny"
      'You use Case Else when there are no other
      'match can be found.
      'IMPORTANT: Don't forget to put the colon at
      'the end of each Case condition.
      Case Else:
        varTrait = "Unknown"
      'You end a Select Case construct with the End
      'Select keywords.
    End Select

    text2.value = varTrait
  End Sub


Figure 3.7 The output from the Select Case statement.

A procedure is a set of statements to which you assign a name. Then, when you want to execute the statements within the procedure you use the name that you assigned to the procedure. VBScript supports two kinds of procedures. The first kind is a Sub. The second kind is a Function.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.