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