Select...Case ステートメント (Visual Basic)Select...Case Statement (Visual Basic)
式の値に応じて、いくつかのステートメント グループのいずれかを実行します。Runs one of several groups of statements, depending on the value of an expression.
構文Syntax
Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
指定項目Parts
用語Term | 定義Definition |
---|---|
testexpression |
必須です。Required. 式。Expression. 基本データ型 (Boolean 、Byte 、Char 、Date 、Double 、Decimal 、Integer 、Long 、Object 、SByte 、Short 、Single 、String 、UInteger 、ULong 、および UShort ) のいずれかとして評価される必要があります。Must 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 To キーワードを使用して、testexpression の一致する値の範囲の境界を指定します。Use 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 .比較演算子 ( = 、<> 、< 、<= 、> 、または >= ) と共に Is キーワードを使用して、testexpression の一致する値に対する制限を指定します。Use the Is keyword with a comparison operator (= , <> , < , <= , > , or >= ) to specify a restriction on the match values for testexpression . Is キーワードが指定されていない場合は、自動的に comparisonoperator の前に挿入されます。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 = expression として評価されます。This form is evaluated as testexpression = expression .expressionlist の式は、testexpression の型に暗黙的に変換可能であり、一緒に使用される 2 つの型に対して適切な 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. testexpression が expressionlist 内のいずれかの句と一致する場合に実行される、Case の後の 1 つ以上のステートメント。One or more statements following Case that run if testexpression matches any clause in expressionlist . |
elsestatements |
任意。Optional. testexpression が どの Case ステートメントの expressionlist 内のどの句とも一致しない場合に実行される、Case Else の後の 1 つ以上のステートメント。One 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. |
RemarksRemarks
testexpression
が Case
expressionlist
句と一致する場合、その Case
ステートメントの後のステートメントは、次の Case
、Case Else
、または End Select
ステートメントまで実行されます。If 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
句の expressionlist
句と一致する場合は、最初の一致の後のステートメントのみが実行されます。If testexpression
matches an expressionlist
clause in more than one Case
clause, only the statements following the first match run.
Case Else
ステートメントを使用して、他のいずれかの Case
ステートメントで testexpression
句と expressionlist
句の間に一致が検出されなかった場合に実行する elsestatements
を導入します。The 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. 必須ではありませんが、予期しない testexpression
値を処理するために、Select Case
コンストラクションに Case Else
ステートメントを指定することをお勧めします。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
句が testexpression
に一致せず、Case Else
ステートメントが存在しない場合、制御は End Select
の後のステートメントに渡されます。If 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
および Case Else
ステートメントで使用される Is
キーワードは、オブジェクト参照の比較に使用される Is Operator と同じではありません。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
は、"apples" と完全に等しいか、アルファベット順の "nuts" と "soup" の間の値を持つか、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
では、文字列 "Apples" と "apples" は等しいものとして比較されますが、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
または Case Else
ステートメント ブロック内のコードがブロック内のステートメントをこれ以上実行する必要がない場合は、Exit Select
ステートメントを使用してブロックを終了できます。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
ステートメントが必要です。また、入れ子になっている外側の Select Case
コンストラクションの 1 つの 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
次の例では、Select Case
コンストラクションを使用して、変数 number
の値に対応する行を記述します。The following example uses a Select Case
construction to write a line corresponding to the value of the variable number
. 2 番目の Case
ステートメントには、number
の現在の値と一致する値が含まれています。そのため、"Between 6 and 8, inclusive" を記述するステートメントが実行されます。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