A decision structure that is similar to If...ElseIf tests for a match against a discrete value. This Select...Case statement has the general format shown below.
A test value is an expression that resolves to a single data value. Each of the match values in the Case statements is one of the possible values that matches the test value. The statements within the matching Case are executed. A Case Else option can be provided for a non-match.
The following example is a simple implementation of Select...Case to give you an idea of how it works.
Sub Check_Color (Src As Object, Args As EventArgs) Select Color.SelectedItem.Value Case "Red" Message.Text = "You chose Red." Case "Blue" Message.Text = "You chose Blue." Case"Green" Message.Text = "You chose Green." Case Else Message.Text = "You chose no color." End Select End Sub <asp:DropDownList id="Color" runat="server"> <asp:ListItem Value="Red" Text="Red"/> <asp:ListItem Value="Blue" Text="Blue"/> <asp:ListItem Value="Green" Text="Green"/> <asp:ListItem Value="None" Text="None of the above"/> </asp:DropDownList> <asp:Button OnClick="Check_Color" Text="Select" runat="server"/> <asp:Label id="Message" runat="server"/>
Red Blue Green None of the above
A selection from the drop-down list produces one of four discrete string values. This value, Color.SelectedItem.Value, is resolved in the Select statement and tested against the possible values of the selection in the Case statements. A match between the selected value and one of the match values produces an appropriate message.
When setting up a Select...Case decision structure you must make sure that the Select expression resolves to one of the basic data types -- a single string or numeric value. The matching Case values can be single values of the same data type as in the above example, or they can be a list of values, a range of values, or a relational test that restricts a selection of values.
Matching Lists. To specify a list of matching values for a particular Case, separate the values with commas.
Case 1, 2, 3 ... Case 4, 5, 6 ... Case 7, 8, 9, 10
Matching Ranges. To specify a range of matching values for a particular Case, use an expression in the format "value To value" to indicate the range. Any value that falls within the collating sequence of the range matches the Case.
Case "A" To "I" ... Case "J" To "R" ... Case "S" To "Z"
It is not necessary to use single character strings. A match can be made on "soup" To "nuts" or "alpha" To "omega", testing for a value that falls within the sorted range of words.
Matching Expressions. To specify a relational test to specify a matching value, use an expression in the format "Is operator value" where operator is one of the relational operators (=, <>, <, >, <=, >= ; the string operator Like cannot be used) and value is a data item of the same Select type.
Case Is < Some_Value ... Case Is = Some_Value ... Case Is > Some_Value
Also, you can combine any of the single-value, list-value, range-value, and expression-value tests for a single Case:
Case 1, 2, 3 To 9, Is < 10