LotusScript 學習筆記2
三、數據類型,常量和變量(續)
常量和變量
當你定義一個變量或者常量時,LotusScript會給予常量和變量默認的作用於和生命週期。
統一作用域內的變量和方法不可以重名。
常量存儲的是在編譯時期就已知值的地址。並且在程序運行過程中不能修改。LotusScript內置常量:
Nothing 變量初始值默認爲nothing
Null
Empty
PI 3.1415926...
True&False 布爾值
變量定義:[Dim|private|public] varName As dataType
一次定義多個變量: [Dim|Public|Private] varName1 As dataType,varName2 As dataType,...
變量後綴:
Suffix Data type
% Integer
& Long
! Single
# Double
@ Currency
$ String
變量初始值:數據類的初始值爲0(Boolean, Byte, Integer,Long,Single,Double, Currency);String的初始值爲""或者 the Null character
數組
數組最大維度爲8。每一層維度的上下限爲:-32768 to 32767
數組內的元素必須是同一類型的。
定義:
DIM a(5) as Integer 數組有六個元素,從0到5。默認的數組下標開始爲0.也可以使用Option Base * 來設置其默認值。
Option Base 0
’Dim empSpacesA(0 To 119, 0 To 3) As String
Dim empSpacesA(119, 3) As String
Option Base 1
'Dim empSpacesA(1 To 120, 1 To 4) As String
Dim empSpacesA(120, 4) As String
Option Base 0
Dim myStats(3, 1 To 2, -2 To 2) As Currency
'The first dimension of this 4 x 2 x 5 array is 0 To 3.
Dim states(1 to 50) As String
Dim statesAnd10Cities(1 to 50, 1 to 10) As String
Dim statesAnd10CitiesAndPeople(1 to 50, 1 to 10, 1 to 3) As Double
定義時可以使用As dataType 或者後綴
Dim aStringArray(1 To 10) As String等同於Dim aStringArray$(1 To 10)
當後綴和As dataType都不使用時,LotusScript會查看數組名中是否包含可識別的dataType。若有那數組便是這個dataType,若是沒有那數組便是Variant type。例如:
'Declare an array of integers.
Dim arrayOfInts(1 To 10)
'Declare an array of Variants.
Dim otherArrayV(1 To 10)
數組的存儲空間大小:
Dim arrayOfSingles(1 To 5, 1 To 10, 1 To 2) As Single
arrayOfSingles的存儲空間大小爲400bytes
Dim myStats(1980 To 1983, 1 To 4, -2 To 2) As Currency
0~3,1~4,-2~2爲4,4,5,所以總共有80個元素
myStats的存儲空間大小爲640bytes
同時我們可以看出,每一維下標的下限並不一定是0或者1.但一般都會設置成0或者1。
LBound方法可以獲取數組的lower bound
LBound ( arrayName [ , dimension ] )
Example:
Option Base 1
Dim myStats(1980 To 1983, 2, -2 To 2) As Currency
Print LBound(myStats)
'Output: 1980 (the lower bound of the first dimension).
Print LBound(myStats, 2)
'Output: 1 (the lower bound of the second dimension).
同理,可以調用Ubound方法獲取數組的upper bound
在分配或者引用數組的值的時候,取決於數組元素的數據類型。
For example:
Dim empSpacesA(120,4) As String
Dim counter As Integer
Dim LB1 As Integer
Dim LB2 As Integer
'Get lower bound of first dimension.
LB1% = LBound(empSpacesA, 1)
'Get lower bound of second dimension.
LB2% = LBound(empSpacesA, 2)
'For the first 40 elements in the first dimension,
'assign the value "Floor 1" to the first element
'in the second dimension; for the next 40 elements
'in the first dimension, assign the value "Floor 2"
'to the first element in the second dimension; and
'for the last 40, assign the value "Floor 3".
For counter% = LB1% to LB1% + 39
empSpacesA(counter%, LB2%) = "Floor 1"
empSpacesA(counter% + 40, LB2%) = "Floor 2"
empSpacesA(counter% + 80, LB2%) = "Floor 3"
Next
動態數組的定義
Dim myDynamicArray() As String
這種定義,只是定義了數組的名稱,沒有長度和維度的數組是不可用的,所以我們利用ReDim聲明來重定義數組。
ReDim [ Preserve ] arrayName ( bounds ) [ As dataType]
Option Base 1
'Declare a dynamic String array. Later, this is
'defined as a one-dimensional array whose elements
'are assigned values that the user enters.
Dim myNames() As String
Dim ans1 As Integer
Dim ans2 As Integer
Dim counter As Integer
Dim userInput As String
'Ask the user to enter a number and assign it to ans1%.
ans1% = CInt(InputBox$ ("How many names would you like to enter?"))
'Use ans1% as the upper bound of the array’s only dimension.
ReDim myNames(ans1%)
'Elicit ans1% strings from the user, and assign them
'to successive elements in the array.
For counter% = 1 to ans1%
myNames(counter%) = InputBox$("Enter a name: ")
Next
'Print the contents of the array on a single line
'with a space between the value of each element.
For counter% = 1 to ans1%
Print myNames(counter%) " " ;
Next
'Output: a newline
Print ""
'Ask the user for another number and assign it to ans2%.
ans2% = CInt(InputBox$("How many more names?"))
'If the number is greater than 0, resize the
'array, preserving its original values, so that the
'user can enter additional values.
If ans2% > 0 Then
ReDim Preserve myNames(ans1% + ans2%)
'Elicit the new values and assign them to the
'elements that have been allocated after the old ones.
For counter% = 1 to ans2%
myNames(counter% + ans1%) = InputBox$("Enter a name: ")
Next
'Print the contents of the array on a single line
'with a space between the value of each element.
For counter% = 1 to ans1% + ans2%
Print myNames(counter%) " " ;
Next
Print ""
End If
若ReDim數組時帶有了Preserve關鍵字,下次ReDim時就只能修改數組最後一維下標的上限數值。數組的維數和數據類型無法再更改。
若使用Erase關鍵字,數組的元素會被重置(to zeros, empty strings,EMPTY, or NOTHING, depending on the data type of the array’s elements)
關於數組的幾個函數:
IsArray()
DataType()
TypeName()
Example
'Declare arrays with a base of 1 and containing 10 elements
Dim myDblArray(1 To 10) As Double
Dim anIntArray(1 To 10) As Integer
Dim counter As Integer
'Seed the random number generator.
Randomize
'Populate myDblArray with random numbers
'greater than 0 and less than 1.
For counter% = 1 To 10
myDblArray(counter%) = Rnd()
Next
'Populate anIntArray with the elements of myDblArray
'after rounding to one decimal place, multiplying
'by 10, dividing by 10 and adding 1 to the remainder
'to yield a whole number between 1 and 10.
For counter% = 1 To 10
anIntArray(counter%) = ((Round(myDblArray(counter%), 1) * 10) Mod 10) + 1
Next
'Test the first element of anIntArray for its data type.
Print TypeName(anIntArray(1))
'Output: INTEGER
'Print the contents of myDblArray and anIntArray.
For counter% = 1 To 10
print myDblArray(counter%) & " " & anIntArray(counter%)
Next
'Output: something like the following:
'.402520149946213 5
'.530154049396515 6
'.309299051761627 4
'5.76847903430462E-02 2
'2.41877790540457E-02 1
'.988802134990692 1
'.688120067119598 8
'.493557035923004 6
'.28598952293396 4
'.610387742519379 7
Dim aStringArray(1 to 5, 1 to 2)
aStringArray(1,1) = "Roman"
aStringArray(1,2) = "Minsky"
aStringArray(2,1) = "Sara"
aStringArray(2,2) = "Nala"
aStringArray(3,1) = "Raymond"
aStringArray(3,2) = "Nala"
aStringArray(4,1) = "Sandra"
aStringArray(4,2) = "Brooks"
aStringArray(5,1) = "Simon"
aStringArray(5,2) = "Anders"
'Check to see if the first two characters of each element
'in the first dimension of aStringArray would be SA
'if they were uppercase. If so, print the corresponding
'element in the second dimension of the array, making
'its first character uppercase and the rest lowercase.
For counter% = 1 to 5
If UCase$(Left$(aStringArray(counter%, 1), 2)) = "SA" Then
Print UCase$(Left$(aStringArray(counter%, 2), 1))& LCase$(Mid$(aStringArray(counter%, 2), 2, Len(aStringArray(counter%, 2))))
End If
Next
'Output:
'Nala
'Brooks