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數據類型方便用戶自由的操作各種數據類型的數值,使得數組和列表可以包含不同數據類型的元素。顯著地擴展了用戶自定義數據類型。