LotusScript 学习笔记4

来源:互联网 发布:智能小车惯性导航算法 编辑:程序博客网 时间:2024/06/07 04:42
LotusScript 学习笔记4


四、表达式和运算符


五、Procedures:Functions, Subs, and Properties


用户可以在模块内或者用户自定义类中创建函数,子元素和属性。我们来看一下格式。

Procedures:是可以通过名称调用的一节已命名的脚本程序。
function是有返回值的已命名的procedure;sub是执行一个或者更多操作的,无返回值的procedure;property是不想暴露在整个应用程序中的,可以间接操作的属性。

LotusScript中有很多内建的函数可以用来执行常用的数据操作。

Function 定义:
[Public|Private] [Static] Function functionName [(parameters)][As dataType]

参数的值传递和引用传递
默认为引用传递,当你引用传递参数时,传递的是指向内存中数据的指针。当函数改变参数值的时候,原始的值也会被改变。
值传递,传递的是内存中数值的副本,当函数改变参数值的时候,原始的值不会被改变。


Examples

Example 1函数的定义和调用
'Define a function FOver with three Integer parameters:
'a variable, an array variable, and a list variable.
Function FOver(a As Integer, b() As Integer, c List As Integer)
'...
End Function
Dim x As Integer
Dim y(5) As Integer
Dim z List As Integer
'Call the function FOver correctly, with arguments
'whose types match the types of the declared parameters.
Call FOver(x, y, z)

Example 2
数组参数,值传递
'Define a function GLevel with one Integer list parameter.
Function GLevel (b List As Integer)
'...
End Function
Dim z List As Integer
'Call the function GLevel incorrectly, passing a list
'argument by value.
Call GLevel ((z))
'Output:
'Error: Illegal pass by value: Z
'A list argument cannot be passed by value.

Example 3
ByVal关键字表示参数必须是值传递
'Define a function FExpr with two Integer parameters;
'the second must always be passed by value.
Function FExpr(a As Integer, ByVal b As Integer)
'...
End Function
Dim x As Integer, w As Integer
Dim y(5) As Integer
Dim z List As Integer
'Call the function FExpr correctly with two Integer
'arguments: a constant and a variable.
Call FExpr(TRUE, x)
'Both arguments are passed by value:
'the first, TRUE, because it is a constant;常量进行的乃是值传递
'and the second, x, because of the ByVal declaration
'in FExpr.
'The following call produces two error messages:
Call FExpr(TRUE, y)
'Output:错误的调用,实参与形参的数据类型不同。
'Error: Illegal pass by value: Y
'Error: Type mismatch on: Y
'Because Y is an array variable, it is an illegal argument to
'pass by value and its type does not match the declared
'parameter type.

Example 4
值传递,引用传递
'When a function modifies one of its parameters,
'the argument value is changed after the function returns
'if the argument was passed by reference. The value is not
'changed if the argument was passed by value.
Function FTRefOrVal(a As Integer) As Integer
FTRefOrVal = a + 1
a = a + 5
End Function
Dim x As Integer, y As Integer
'Show results of passing argument by reference.引用传递
Print x, FTRefOrVal(x), x
'Output:
'0 1 5
'The value of x was changed from 0 to 5 in FTRefOrVal.
'Show results of calling with argument by value
'(note the extra parentheses around y%).值传递
Print y, FTRefOrVal((y)), y
'Output:
'0 1 0
'The value of the copy of y was changed from 0 to 5
'in FTRefOrVal. The value of y is unchanged.

函数的返回值:
FunctionName = returnValue,
For example:
Function Cubit(intArg%) As Double
'Return the cube of intArg%.
Cubit# = intArg% ^ 3
End Function
or
Function Left5(aString As String) As String
'Return the leftmost 5 characters of aString$.
Left5$ = Left$(aString$, 5)
End Function

如果函数的返回值需要数组或者列表,你需要把返回值的类型定义成Variant.若是不定义返回值,那么返回值为默认,数字类的数据类型的话默认为0,字符类的默认为"".

Example
传入fistName,space,lastName的String 数组,返回lastName,space,fistName的String 数组

Dim myVariantVarV As Variant
Dim anArray(1 to 3) As String
Dim X As Integer
anArray$(1) = "Alex Smith"
anArray$(2) = "Elizabeth Jones"
anArray$(3) = "Martin Minsky"
Function SwitchNames(arrayOfNames() As String) As Variant
'Declare a local array variable to pass back to the
'application as the return value of SwitchNames.
'Performing the operation on arrayOfNames, which is
'passed by reference, would change anArray if
'arrayOfNames were the return value of the function.
Dim newArrayOfNames(1 to 3) As String
Dim tempArray(1 to 2, 1 to 3) as String
Dim aSpace As Integer
For X% = 1 to 3
'Locate the space that separates first name from
'last name in arrayOfNames, then extract everything
'before the space to tempArray, then extract
'everything after the space to the corresponding
'location in tempArray’s second dimension.
aSpace% = Instr(arrayOfNames$(X%), " ")
tempArray$(1, X%) = Mid$(arrayOfNames$(X%), 1 , aSpace% - 1)
tempArray$(2, X%) = Mid$(arrayOfNames$(X%), aSpace% + 1,Len(arrayOfNames$(X%)))
Next
For X% = 1 to 3
newArrayOfNames(X%) = tempArray(2, X%) & ", " & tempArray(1, X%)
Next
SwitchNames = newArrayOfNames
End Function
MyVariantVarV = SwitchNames(anArray())
For X% = 1 to 3
print myVariantVarV(x%)
Next
'Output: Smith, Alex
'Jones, Elizabeth
'Minsky, Martin
For x% = 1 to 3
Print anArray(x%)
Next
'Output: Alex Smith
'Elizabeth Jones
'Martin Minsky



0 0
原创粉丝点击