LOTUSSCRIPT 言語


Select Case ステートメントを使用して選択する
ブロックステートメントの Select Case は、式の値に応じて、1 つ以上のステートメントのグループからいずれか 1 つのグループを選択して実行するよう指定します。If...Then...ElseIf ステートメントに似ています。

構文は次の通りです。

Select Case selectExpr

End Select

実行時に、Select Case ステートメントは 1 つの selectExpr 式の値を各 conditionList で設定される値と比較します。selectExpr の値に最初に一致した conditionListstatements を実行します。結果として、ステートメントのグループが 1 つだけ実行されるか、またはどれも実行されないことになります。Case■Else 句が含まれている場合、これは selectExpr がすべての条件リスト内のすべての条件にあてはまらない場合に実行されます。句が実行された後、LotusScript は、End Select ステートメントに続く最初のステートメントから実行を続行します。

次の例は、整数に接尾辞を追加して序数にします。スクリプトは関数 SetOrd を定義して、呼び出します。この関数は文字列の引数を受け取り、正しい種類かどうかを判断し、引数についてのメッセージか、または正しい接尾辞の付いた引数を示す文字列を返します。

Function SetOrd (anInt As String) As String
  Dim printNum As String
  ' If argument can't be converted to a number,
  ' assign a message and do nothing more.
  If Not IsNumeric(anInt$) Then
     SetOrd$ = "That's not a number."
     Exit Function
  ' If argument is not a whole number,
  ' assign a message and do nothing more.
  ElseIf Fraction(CSng(anInt$)) <> 0 Then
     SetOrd$ = "That's not a whole number."
     Exit Function
  ' If number is not in range, assign a message
  ' and do nothing more.
  ElseIf CInt(anInt$) > 50 Or CInt(anInt$) < 0 Then
     SetOrd$ = "That number's out of range."
     Exit Function
  End If
  ' Determine and append the correct suffix.
  Select Case anInt$
     Case "1", "21", "31", "41":printNum$ = anInt$ & "st"
     Case "2", "22", "32", "42":printNum$ = anInt$ & "nd"
     Case "3", "23", "33", "43":printNum$ = anInt$ & "rd"
     Case Else:printNum$ = anInt$ & "th"
  End Select
  SetOrd$ = "This is the " & printNum$ & " number."
End Function
' Call the function.
MessageBox(SetOrd(InputBox$("Enter a whole number between" & _
  " 0 and 50:")))

例の最後の行は、関数 SetOrd の外にある唯一の実行可能コードであり、InputBox$ 関数で受け取ったユーザー入力に基づくメッセージを表示するよう MessageBox ステートメントに指示します。ユーザーによって入力された値は SetOrd に渡され、そこで MessageBox で表示されるものが決定されます。

関連項目