LOTUSSCRIPT 言語


Variant 型
Variant 型は特殊なデータ型です。Variant 型の変数は、LotusScript で認識される次のデータ型のいずれの値も保持できます。ただし、ユーザー定義のデータ型を除きます。
Variant 型の変数は、明示的または暗黙的にスカラー変数を宣言するのと同様に宣言します。Deftype ステートメントが適用されない場合、As dataType 節やデータ型の接尾辞を使用せずに宣言した変数は Variant 型になります。ここで、Variant 型の変数は、オブジェクト参照変数やユーザー定義のデータ型の変数と区別するために、接尾辞 V を付けて表します。以下に例を示します。

Dim myVariant1V As Variant
Dim myVariant2V
Public myVariant3V As Variant
myVariant4V = 123.45

Variant 型の変数を明示的に宣言すると、LotusScript はその変数を特殊な値 EMPTY で初期化します(Variant 変数にこの値が入っているかどうかを調べるには、IsEmpty 関数を使用します)。変数を Variant 型で宣言することは、他のデータ型を割り当てるほど効率的ではありませんが、便利です。Variant 型の変数に値を代入するとき、LotusScript は提供された情報に応じて次のいずれかの方法でその値のデータ型を識別します。


Variant 型の変数に代入された値のデータ型は、次の例のように DataType または TypeName 関数を使用して識別できます。

Dim numVarV As Variant
Dim anAmount As Currency
anAmount@ = 20.05
numVarV = anAmount@
Print TypeName(numVarV)
' Output:CURRENCY
numVar = 20.05
Print TypeName(numVar)
' Output:DOUBLE 型

特定の状況下では、Variant 型の変数に代入された値のデータ型は、特定の操作の要求に応じて変更できます。たとえば、次の例では、ユーザーが一連の数字を入力した場合、ある操作では文字列値として、また別の操作では数値として扱われます。

' Declare a Boolean variable and assign it an initial
' value of FALSE (0).The application subsequently tests
' this variable, taking appropriate action depending on the
' variable's value裕rue (-1) or False (0).
quitFlag = FALSE
Dim ansV As Variant
' Have the user enter some numeric characters.
ansV = InputBox("Enter a number.")
' See how many characters the user entered
' and assign that number to the Integer variable
' UB%.This involves treating the value of ansV
' as a String.
UB% = Len(ansV)
' Test the value of ansV to see if it can be
' interpreted as being of one of the numeric
' data types.If so, declare a dynamic array of Variants,
' then allocate space for as many elements as
' there are characters in ansV, and then assign
' the successive digits in ansV to the elements in
' the array.
If IsNumeric(ansV) = True then
 Dim digitArrayV() As Variant
 ReDim digitArrayV(1 To UB%)As Variant
 For x% = 1 to UB%
    digitArrayV(x%) = Mid(ansV, x%, 1)
 Next
Else
  Print "You entered some nonnumeric characters."
  quitFlag = TRUE
End If

' If ansV was able to be interpreted as a numeric,
' print its digits and their sum; then print
' the result of adding that sum to the original
' number that the user entered.
If quitFlag = False Then
  Dim theSum As Integer
  ' theSum% is initialized to 0.
  For x% = 1 to UB%
     theSum% = theSum% + digitArrayV(x%)
     Print digitArrayV(x%) ;
  Next
  Print ""
  Print "Their sum is:" & theSum%
  Print "Their sum added to the original number is:" _
    & ansV + theSum%
End If
' Output, supposing the user enters 12345:
' 12345
' Their sum is:15
' Their sum added to the original number is: 12360

関連項目