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

視運算式的值而定, 執行數個語句群組的其中一個。Runs one of several groups of statements, depending on the value of an expression.


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


詞彙Term 定義Definition
testexpression 必要項。Required. 運算式.Expression. 必須評估為Boolean其中一個基本資料類型 (、 Decimal Double Date Byte Char 、、、Long、 、、SByte、、、、 、、、、、、、、、Short Integer Object``Single、 、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值範圍的界限。 ToUse the To keyword to specify the boundaries of a range of match values for testexpression. 的值expression1必須小於或等於的expression2值。The value of expression1 must be less than or equal to the value of expression2.

< <> <=使用關鍵字搭配>=比較=運算子 (、、 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.


如果testexpression符合 any Case End Select Case Case Else Case子句, 則語句後面的語句會執行到下一個、或語句。 expressionlistIf testexpression matches any Case expressionlist clause, the statements following that Case statement run up to the next Case, Case Else, or End Select statement. 控制項接著會傳遞至後面End Select的語句。Control then passes to the statement following End Select. 如果testexpression 符合一個以上Case子句中的子句,則只會執行第一個相符專案之後的語句。expressionlistIf testexpression matches an expressionlist clause in more than one Case clause, only the statements following the first match run.

如果Case Else在任何其他elsestatements testexpression語句的和expressionlist子句之間找不到相符項, 則會使用語句來導入要執行的。 CaseThe Case Else statement is used to introduce the elsestatements to run if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. 雖然並非必要, 但在您Case Else Select Case的結構中有語句來處理testexpression未預期的值是個不錯的主意。Although not required, it is a good idea to have a Case Else statement in your Select Case construction to handle unforeseen testexpression values. 如果沒有Case expressionlist End Select符合的子句,而且沒有語句,則控制權會傳遞至下列語句。testexpression Case ElseIf no Case expressionlist clause matches testexpression and there is no Case Else statement, control passes to the statement following End Select.

您可以在每個Case子句中使用多個運算式或範圍。You can use multiple expressions or ranges in each Case clause. 例如, 下列程式程式碼是有效的。For example, the following line is valid.

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.

您可以為字元字串指定範圍和多個運算式。You can specify ranges and multiple expressions for character strings. 在下列範例中, Case會比對完全等於「蘋果」的任何字串、以字母順序表示「螺母」和「湯品」之間的值, 或包含與目前值完全相同的testItem值。In the following example, Case matches any string that is exactly equal to "apples", has a value between "nuts" and "soup" in alphabetical order, or contains the exact same value as the current value of testItem.

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

的設定Option Compare可能會影響字串比較。The setting of Option Compare can affect string comparisons. Option Compare Text下, 字串 "蘋果" 和 "蘋果" 比較為相等, 但在Option Compare Binary之下則不會。Under Option Compare Text, the strings "Apples" and "apples" compare as equal, but under Option Compare Binary, they do not.


具有Case多個子句的語句可以呈現稱為「最少運算」的行為。A Case statement with multiple clauses can exhibit behavior known as short-circuiting. Visual Basic 會從左至右評估子句, 而且如果其中一個會產生符合testexpression的, 則不會評估其餘的子句。Visual Basic evaluates the clauses from left to right, and if one produces a match with testexpression, the remaining clauses are not evaluated. 最短運算可以改善效能, 但如果您預期要評估中的expressionlist每個運算式, 可能會產生非預期的結果。Short-circuiting can improve performance, but it can produce unexpected results if you are expecting every expression in expressionlist to be evaluated. 如需有關簡短運算的詳細資訊, 請參閱布林運算式For more information on short-circuiting, see Boolean Expressions.

如果Case Exit SelectCase Else語句區塊內的程式碼不需要執行區塊中的任何其他語句, 它可以使用語句來結束區塊。If the code within a Case or Case Else statement block does not need to run any more of the statements in the block, it can exit the block by using the Exit Select statement. 這會立即將控制權轉移給後面End Select的語句。This transfers control immediately to the statement following End Select.

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.


下列範例會使用Select Case結構來撰寫對應至變數number值的線條。The following example uses a Select Case construction to write a line corresponding to the value of the variable number. 第二Case個語句包含符合目前number值的值, 因此會執行寫入「介於6和 8 (含)」的語句。The second Case statement contains the value that matches the current value of number, so the statement that writes "Between 6 and 8, inclusive" runs.

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

另請參閱See also