LOTUSSCRIPT 言語
構文を以下に示します。
[ Public | Private ] Type typeName
ユーザー定義のデータ型にはプロシージャ (プロパティとメソッド) を含めることはできません。また、ユーザー定義のデータ型は拡張することもできません。
次の例は、従業員情報についてのデータベースレコードを保持するために、3 つのメンバ変数 (ID、lastName、firstName) を含む Employee データ型を作成する方法を示しています。
ユーザー定義のデータ型の変数を宣言する
ユーザー定義のデータ型を定義した後、メンバ変数を宣言できます。
以下に例を示します。
Dim President As Employee ' Create a single employee record.
多くのデータベースレコードからのデータを入れる場合、メンバ変数の配列を宣言できます。
Dim Staff(10) As Employee ' Create an array of ten employee ' records.
メンバ変数を参照する
メンバ変数を参照するには、ドット表記法 (object.memberVariable) を使用します。値を割り当てるには、代入ステートメントを使用します。
President.ID = 42 President.lastName = "Wilkinson" President.firstName = "May"
配列またはリストのメンバ変数の要素を参照することもできます。
Staff(1).ID = 1134 Staff(1).lastName = "Robinson" Staff(1).firstName = "Bill" Staff(2).ID = 2297 Staff(2).lastName = "Perez" Staff(2).firstName = "Anna"
メンバ変数の値を変数に代入するかメンバ変数の値を出力することにより、メンバ変数からデータを取り出すことができます。
Dim X As String X$ = Staff(2).lastName Print X$ ' Prints Perez.
メンバ変数の宣言時にメモリを節約する
ユーザー定義のデータ型のメンバは、必ずしも連続したメモリ領域に格納されるとは限りません。最初は最も大きい境界を持つメンバを宣言し、最後に最も小さい境界を持つメンバを宣言することにより、データスペースを効率的に使用できます。定義で無駄になったスペースは、そのユーザー定義のデータ型を持つすべての変数でも無駄なスペースになります。
次の例は、適切に位置合わせされた変数を示しています。
Type T1 m1 As Variant ' 16 bytes m2 As Double ' 8 bytes m3 As Long ' 4 bytes m4 As String ' 4 bytes m5 As Integer ' 2 bytes m6(10) As Integer ' 2 bytes m7 As String * 30 ' 1 byte End Type
ユーザー定義のデータ型の変数は、その変数の中で最大メンバのサイズと等しい境界で格納されます。
次の例は、上の例の続きで、ユーザー定義のデータ型 T1 の各変数が 16 バイト境界で整列される様子を示しています。
Type T2 m1 As T1'16-byte boundary;T1's largest member boundary is 16. m2(3) As Long ' 4 bytes. End Type
メンバ変数を宣言する場合、次のことを考慮してください。
Dim x As Long Dim y As String
この 2 行は次の 2 行と同等です。
Dim y As String Dim x As Long
ファイルに格納されたデータを扱うために、ユーザー定義のデータ型を作成することがあります。たとえば、次のスクリプトは、ユーザー定義のデータ型の配列に従業員の駐車スペース情報を含んだサンプル ASCII ファイルを読み込みます。
Type RecType empID As Double ' Employee ID employee As String ' Employee name theSection As Integer ' Car parking section theSpace As Integer ' Designated parking space theFloor As Integer ' Car parking level End Type ' Dynamic array sizes to fit the lines in the file. Dim arrayOfRecs() As RecType Dim txt As String Dim fileNum As Integer Dim counter As Integer Dim countRec As Integer Dim found As Boolean fileNum% = FreeFile ' Get a file number to open a file. counter% = 0 Open "c:\myfile.txt" For Input As fileNum% Do While Not EOF(fileNum%) Line Input #fileNum%, txt$ ' Read each line of the file. counter% = counter% + 1 ' Increment the line count. Loop Seek fileNum%, 1 ' Pointer to beginning of file ' Since file has counter% number of lines, define arrayOfRecs ' to have that number of elements. ReDim arrayOfRecs(1 To counter%) ' Read the file contents into arrayOfRecs. For countRec% = 1 to counter% Input #fileNum%, arrayOfRecs(countrec%).empID, _ arrayOfRecs(countRec%).employee, _ arrayOfRecs(countrec%).theSection, _ arrayOfRecs(countrec%).theSpace, _ arrayOfRecs(countrec%).theFloor Next Close fileNum% ' Elicit an employee's name and look for it in arrayOfRecs. ans$ = InputBox$("What's your name?") found = False For x% = 1 To counter% If arrayOfRecs(x%).employee = ans$ Then found = True Print "Greetings, " & ans$ & "." Exit For End If Next If found = False Then Print "No such employee.
関連項目