THE SELECT CASE STATEMENT


In many cases, you'll want to compare one value or expression to a whole series of possible values. One way to accomplish this is by setting up as many IfThen statements as are required to make all the comparisons. However, a better way to tackle this type of situation is to use the Select Case statement.

The syntax for the Select Case statement is shown below.

 Select Case expression   Case value     statements     .     .     .   Case value     statements   Case Else     statements End Select 

As you can see, you use the Select Case statement to set up a block of code that begins with a Select Case statement and ends with the End Select statement. Between these two statements, you insert one or more Case statements, each of which defines a value or expression to be tested. You can also include a Case Else statement as a sort of catchall. The Case Else statement will execute in the event that all of the previous Case statements failed to result in a match. To better understand how the Select Case statement works, take a look at the following pseudocode example.

 Select Case Temperature   Case If it is below 32 degrees stay in bed   Case If it is exactly 32 degrees play the lottery   Case Else Just read the newspaper End Select 

In this example, one value, the temperature, is compared to a number of different values. If any of the values, as defined by a Case statement, are equal to the value of the temperature, then the programming logic associated with that value is executed.

As you can see, functionally the Select Case and the IfThenElse statements are fairly similar. However, the Select Case statement is easier to read and is perfect for performing multiple tests against a single value or expression.

In most cases, you'll be able to reduce the number of lines of code that you need to perform a conditional test when you use a Select Case code block in place of a series of IfThen statements. As an example, take a look at the following code, which represents a rewrite of the example presented earlier in this chapter in the section that covered the nesting IfThen statements.

 Select Case intWrong = 3      Case intCount < 2           MessageBox.Show("Game over. Your typing skill level " _                 & "is: Beginner. Please play again!")           intCount = 0           intWrong = 0           Return      Case intCount < 4           MessageBox.Show("Game over. Your typing skill level " _                 & "is: Intermediate. Please play again!")           intCount = 0           intWrong = 0           Return      Case intCount < 5           MessageBox.Show("Game over. Your typing skill level " _                 & "is: Advanced. Please play again!")           intCount = 0           intWrong = 0           Return End Select 

As you can see, this example is not only a few lines shorter than the previous example, but also easier to read and follow along.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net