LOTUSSCRIPT LANGUAGE
The syntax is :
[ Public | Private ] Type typeName
User-defined data types cannot contain procedures (properties and methods) and cannot be extended.
This example shows how you could create an Employee data type that contains three member variables (ID, lastName, and firstName) to hold database records of employee information:
Declaring a variable of a user-defined data type
After you define a user-defined data type, you can declare a member variable.
For example:
Dim President As Employee ' Create a single employee record.
If you want to hold data from many database records, you can declare an array of member variables.
Dim Staff(10) As Employee ' Create an array of ten employee ' records.
Referring to member variables
Use dot notation (object.memberVariable) to refer to member variables. Use an assignment statement to assign values.
President.ID = 42 President.lastName = "Wilkinson" President.firstName = "May"
You can refer to the elements of a member variable that is an array or list:
Staff(1).ID = 1134 Staff(1).lastName = "Robinson" Staff(1).firstName = "Bill" Staff(2).ID = 2297 Staff(2).lastName = "Perez" Staff(2).firstName = "Anna"
You can retrieve data from member variables by assigning a member variable value to a variable or printing the value of a member variable:
Dim X As String X$ = Staff(2).lastName Print X$ ' Prints Perez.
Conserving memory when declaring member variables
Members of a user-defined data type are not necessarily stored in consecutive bytes of memory. You can use data space efficiently by declaring members with the highest boundary first and those with the lowest boundary last. Wasted space in the definition becomes wasted space in every variable of that user-defined data type.
This example shows a well-aligned variable:
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
LotusScript stores a variable of a user-defined data type on a boundary equal to the size of its largest member.
This example, continued from above, shows how each variable of user-defined data type T1 is aligned on a 16-byte boundary.
Type T2 m1 As T1'16-byte boundary;T1's largest member boundary is 16. m2(3) As Long ' 4 bytes. End Type
When you declare member variables:
Dim x As Long Dim y As String
is as efficient as
Dim y As String Dim x As Long
You often create user-defined data types to work with data stored in files. For example, the script below and following illustration read a sample ASCII file that contains employee parking information into an array of user-defined data types:
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.
See Also