LotusScript 学习笔记3
三、数据类型,常量和变量(续)
List 列表
列表是相同类型数据的一维集合。程序运行过程中随时都可以修改。
[Dim|public|private] listname List AS dataType
同样,如果不添加AS dataType选项,可以添加相应数据类型的后缀。
Dim mylist1$ List
Dim mylist2 List As String
另外如果As String和后缀都没有添加,lotusScript会检查listname中是否包含相应的数据类型,没有就认为list为Variant.
Option Compare { Case | NoCase | Binary }
以上操作用来设置list tags是否对大小写敏感。Case或者Binary关键字表示大小写敏感。NoCase表示不敏感。
Example
'Make string comparison case insensitive
'in this module.
Option Compare NoCase
'Declare a list, myList, to hold first names.
'The list tags will be unique IDs.
Dim myList List As String
Dim newTag As String
Dim newValue As String
'Put some elements in the list.
myList("A1234") = "Andrea"
myList("A2345") = "Vera"
myList("A3456") = "Isabel"
'Ask the user to enter an ID and a name.
newTag$ = InputBox$("Please enter your ID:")
newValue$ = InputBox$("Please enter your first name:")
'Add a new element to the list with
'the user’s ID as the list tag and the user’s name as
'the value of the new element.
myList(newTag$) = newValue$
Print myList(newTag$)
'Output: the name that the user entered
TypeName(listName) 返回表示列表数据类型的String TypeName(listName(listTag))返回表示列表某个元素数据类型的String
DataType(listName)返回一个Integer:2048+列表的dataTypeCode DataType(listName(listTag))返回一个Integer:2048+列表listTag位置元素的dataTypeCode
IsList(listName) returns True(-1) or False 0)
IsElement(listName(stringExpr))returns True(-1) or False(0)来判断stringExpr是不是列表中的标签;判断stringExpr的元素是否存在
ListTag(refVar)返回refVar元素的ListTag
Erase listName 移除listName列表中的所有元素并收回之前分配的存储空间
Erase listName(listTag)移除listName列表中的listTag对应的元素并收回之前分配的存储空间
Example
'Declare a list to hold employee IDs.
'The list tags will be the names of the employees.
Dim empList List As Double
'Make absolutely sure empList is Double.
If TypeName(empList) <> "DOUBLE LIST" Then
Print "Warning: empList is " & TypeName(empList)
End If
If DataType(empList) <> 2053 Then
Print "Warning: empList is " & CStr(DataType(empList))
'We expected 2053 (that is, 2048 + 5).
End If
'Declare a String variable for user name.
Dim ans As String
'Declare a Double variable for user ID.
Dim yourID As Double
'Declare an Integer variable to serve as a flag.
Dim found As Boolean
'Create some list elements and assign them values.
empList("Maria Jones") = 12345
empList("Roman Minsky") = 23456
empList("Joe Smith") = 34567
empList("Sal Piccio") = 91234
'Ask the user to enter the name to be removed from the
'list of employees who have been assigned parking spaces.
ans$ = InputBox$("Which employee no longer needs a space?")
'Check to see if the employee’s name appears as a list tag
'in the list. If not, display a message and stop. Otherwise,
'validate the employee’s ID. If everything checks out,
'remove the employee item from the parking list.
If IsElement(empList(ans$)) = True then
Print ans$ & " is a valid employee name."
yourID# = CDbl(InputBox$("What’s " & ans$ & "’s ID?"))
'The following ForAll block does two things:
'it checks to see if yourID# is a valid ID and,
'if so, if it matches the ID for the employee
'whose name is ans$. If so, that element is removed
'(erased) from the list. The found flag is initially
'FALSE (0). If yourID# is a valid ID, found is set to
'TRUE (-1). The variable empID is the reference variable
'in the ForAll loop.
found = FALSE
ForAll empID In empList
If empID = yourID# then
found = TRUE
If ListTag(empID) = ans$ then
Erase empList(ans$)
'Verify the removal of the list element.
If IsElement(empList(ans$)) = FALSE then
Print ans$ & " is no longer on the list."
End If
Else
Print "Valid ID but wrong employee."
End If
'No need to look further for yourID#,
'so get out of the ForAll loop.
Exit ForAll
End If
End ForAll
If found = False then
Print "No such employee ID."
End If
Else
Print "No such employee."
End if
Variants
Variant是一个比较特殊的数据类型。Variant类型的变量,除了用户自定义的数据类型之外,可以存放以下各种类型的值:
LotusScript支持的常用数据类型:Boolean, Byte, Integer, Long, Single,Double, Currency, String;
A date/time value;
An array or list;
An object reference, that is, a pointer to an OLE Automation object or to an instance of a product-defined or user-defined class, or an object reference to a Java Object;
The NULL value;
The EMPTY value.
Variant类型变量的定义:
Dim myVariant1V As Variant
Dim myVariant2V
Public myVariant3V As Variant
myVariant4V = 123.45
如上,Variant类型变量的后缀是V
Example
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, True (-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
布尔值
lotusScript中的布尔值True and False,-1 and 0。 当你把一个布尔值assign给一个variant变量时,这个值可以是True or False 或者-1 or 0
Example
varV = -1
Print varV 'Output : -1
If varV = True Then
Print "varV is True."
Else
Print "varV is False."
'Output: varV is True.
anInt% = 0
If anInt% = True then
Print "True"
Else
print "False"
'Output: False
Dates/Time
lotusScript并没有Dates/Time数据类型,你并不能定义一个Dates/Time类型的变量。但是lotusScript能够识别dates数据,并且提供了一系列的函数来处理dates数据。一般这类数据是用8bytes的float来存储。
Function Statement Purpose
CDat Function 把数字类或者String的date表示转换为date/time Variant 的值表示。
Date Function 返回系统日期
Date Statement 设置系统时间
DateNumber Function 把年,月,日转化成 date value
DateValue Function 把 string 转化成 date value
Day Function 返回一个date/time 表示中的日期信息
FileDateTime Function 返回文件最近修改保存的日期和时间。
Format Function 格式化 a number, a date/time value, or a string
Hour Function 返回 date/time expression 中的小时数
IsDate Function 判断是否是 a Variant date/time value
Minute Function 返回 date/time expression 中的分钟数
Month Function 返回 date/time expression 中的月份
Now Function 返回系统当前的日期和时间
Second Function 返回 date/time expression 中的秒数
Time Function 返回系统时间.日期部分的值被设定为0或者12.30.1899
Time Statement 设置系统时间
TimeNumber Function 把小时,分钟和秒转化为小数表示的 date/time 值
Timer Function 返回午夜过后的秒数
TimeValue Function 把一个 string 转化为小数表示的 date/time 值
Today Function 与Date function相同,返回系统当前时间
WeekDay Function 从date/time expression返回星期几
Year Function 从 date/time expression返回一个四位数整型的年份
两个date/time variant数值相加还是date/time variant,但是两个date/time variant相减便会产生一个Double的Variant.
我们需要使用Cdat函数把这个变量转化成一个date/time value.
Example
假设当前时间是1994.10.26,时间是7:49:23AM
Dim theInstantV As Variant
Dim theDateV As Variant
Dim theDateValV As Variant
Dim myDate As String
获取当前时间并把结果存储在Variant变量中。
theInstantV = Now
Print theInstantV
'Output: 10/26/94 7:49:23 AM
打印其中的日期和小时数
Print Day(theInstantV) & " " & Hour(theInstantV)
'Output: 26 7
当前日期存储在Variant变量中
theDateV = Date
Print theDateV
'Output: 10/26/94
Print theDateV - 1
'Output: 10/25/94
把现在日期转化成Double型
Print CDbl(theDateV)
'Output: 34633
'Convert a value of type Double
'to a date value, assign it to a
'Variant variable, and print it.
theDateV = CDat(34633)
Print theDateV
'Output: 10/26/94
y% = Year(theDateV)
m% = Month(theDateV) + 1
d% = Day(theDateV) + 1
theDateV = DateNumber(y%, m%, d%)
Print theDateV
'Output: 11/27/94
myDate$ = "October 28, 1994"
Print DateValue(myDate$) - 1
'Output: 10/27/94
theDateV = DateValue(myDate$)
'Check the data type of the value
'held by the Variant variable theDateV.
Print TypeName(theDateV)
'Output: DATE
Print Format(DateValue("10-18-14"), "mmm-d-yyyy")
'Output: Oct-18-1914
Variant数据类型方便用户自由的操作各种数据类型的数值,使得数组和列表可以包含不同数据类型的元素。显著地扩展了用户自定义数据类型。