# Select...Case 陳述式 (Visual Basic)Select...Case Statement (Visual Basic)

## 語法Syntax

Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select


## 組件Parts

testexpression 必要項。Required. 運算式.Expression. 必須評估為Boolean其中一個基本資料類型 (、 Decimal Double Date Byte Char 、、、Long、 、、SByte、、、、 、、、、、、、、、Short Integer ObjectSingle、 、StringULong和)。 UInteger UShortMust evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort).
expressionlist 語句中的Case必要項。Required in a Case statement. 表示之符合值的testexpression運算式子句清單。List of expression clauses representing match values for testexpression. 多個運算式子句會以逗號分隔。Multiple expression clauses are separated by commas. 每個子句都可以採用下列其中一種形式:Each clause can take one of the following forms:

- expression1 To expression2- expression1 To expression2
- [ Is ] comparisonoperator expression- [ Is ] comparisonoperator expression
- 運算式- expression

< <> <=使用關鍵字搭配>=比較=運算子 (、、 testexpression、 、或)來指定的比對值限制。> IsUse the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. 如果未提供關鍵字,則會在comparisonoperator之前自動Is插入。If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.

expression指定的表單會被視為Is表單的特殊案例, 其中comparisonoperator是等號 (=)。The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). 這個表單會評估testexpression為 = expressionThis form is evaluated as testexpression = expression.

expressionlist的運算式可以是任何資料類型, 前提是它們會隱含地轉換成的testexpression類型, 而適當comparisonoperator的適用于搭配使用的兩個類型。The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.
statements 選擇性。Optional. 如果符合中Case testexpression 的任何expressionlist子句, 則在之後執行的一個或多個語句。One or more statements following Case that run if testexpression matches any clause in expressionlist.
elsestatements 選擇性。Optional. 如果Case Elsetestexpression 符合任何Case語句之中的任何子句, 則會在執行之後的一或多個語句。expressionlistOne or more statements following Case Else that run if testexpression does not match any clause in the expressionlist of any of the Case statements.
End Select 結束的定義Select...Case結構。Terminates the definition of the Select...Case construction.

## 備註Remarks

Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber

Case Is 語句Case Else中所使用的關鍵字與is 運算子不同, 後者是用於物件參考比較。The Is keyword used in the Case and Case Else statements is not the same as the Is Operator, which is used for object reference comparison.

Case "apples", "nuts" To "soup", testItem

Select Case可以嵌套結構。Select Case constructions can be nested. 每個Select Case嵌套的結構都必須End Select有相符的語句, 而且必須完全包含Case在其所用之外部Select Case結構的單一或Case Else語句區塊內。Each nested Select Case construction must have a matching End Select statement and must be completely contained within a single Case or Case Else statement block of the outer Select Case construction within which it is nested.

## 範例Example

Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Not between 1 and 10, inclusive")
End Select