LotusScript 学习笔记5

五、Procedures:Functions, Subs, and Properties(续)


用户自定义的函数

没有参数的函数可以直接通过函数名调用;
Dim anInt As Integer
Dim aDouble As Double
Function Cubit1 As Double
'Return the cube of anInt% and display a message
'saying what that value is.
Cubit1# = anInt% ^ 3
Print anInt% & " cubed = " & Cubit1# & "."
End Function
anInt% = 4
aDouble# = Cubit1#
'Output: 4 cubed is 64.

带参数的函数的调用:
Function Cubit2(X As Integer) As Double
....
End Function

Call Cubit2#(anInt%) 
Cubit2# anInt%
Cubit2#(anInt%)


Function Cubit3(X As Integer, Y As Integer) As Double
....
End Function

Call Cubit3#(anInt%, anotherInt%)
Cubit3# anInt%, anotherInt%
Cubit3# (anInt%, anotherInt%)


递归函数

阶乘函数:
Function Facto# (theNum%)
'Calculate theNum% factorial and make it
'the return value of Facto#.
If theNum% <= 0 Then
Facto# = 0
ElseIf theNum% = 1 Then
Facto# = 1
Else
Facto# = theNum% * Facto#(theNum% -1)
End If
End Function

没有参数的递归函数调用时,必须在函数名后面添加空的括号。
没有括号的函数名,被视作函数返回值变量。

Function Recurse As Integer
'...
'Call Recurse and assign the return value to x.
x = Recurse()
'...
'Assign the current value of the Recurse variable to x.
x = Recurse
'...
End Function


Subs

Sub是一个命名了的,包含一些列操作的没有返回值的过程。

[Public|Private] [Static] Sub subName [(parameters)]
.....
End Sub

对于没有参数的Sub,可以通过函数名直接Call调用,也可以通过函数名加空括号Call调用。
For example:
Dim aName As String
Sub PrintName1
'Make the contents of firstName$ be all uppercase
'and display the result.
firstName$ = UCase$(firstName$)
Print firstName$
End Sub
firstName$ = "David"
Call PrintName1()
'Output: DAVID
Call PrintName1
'Output: DAVID


只有一个参数的Sub调用:
Call PrintName2(firstName$) 'Call Sub名 (参数)单括号
Call PrintName2((firstName$))'Call Sub名 ((参数))双括号
PrintName2(firstName$)'直接 Sub名(参数)
PrintName2 firstName$ '直接 Sub名 参数

多个参数的Sub
Call PrintName3(firstName$, lastName$)
PrintName3 firstname$, lastName$


专有的四个Sub
Sub Initialize在加载模块的时候做一系列初始化操作。
Sub Initialize
....
End Sub

Sub Terminate当应用程序卸载模块时,执行一系列清理操作。
Sub Terminate
....
End Sub

Sub New and Sub Delete是用户自定义类中的专有。

属性Properties
函数内部定义的变量,外部可以通过Get和Set方法进行访问和赋值。

定义Property
[Public|Private] [Static] Property Set propertyName [As dataType]
......
End Property
and
[Public|Private] [Static] Property Get propertyName [As dataType]
......
End Property

Example

%Include "LSCONST.LSS"
Dim anInt As Integer
Dim aDouble As Double
Dim bigNum As Double

Property Set theCube As Double
anInt% = theCube#
End Property

Property Get theCube As Double
aDouble# = anInt% ^ 3
If aDouble# > bigNum# Then
bigNum# = aDouble#
End If
theCube# = anInt%
End Property

Sub KeepGoing
Dim goAgain As Boolean
Dim msg As String
Dim msgSet As Integer
Dim more As Integer
goAgain = TRUE
msg$ = "Want to go again?"
msgSet% = MB_YESNO + MB_ICONQUESTION

'用户输入一个数值,并将这个数值通过执行Property Set theCube# 进行赋值,通过执行Property Get theCube#计算立方值。并将这个值与bigNum#进行比较,若大于bigNum#则将这个值赋给bigNum#。然后重复这个过程。

While goAgain = True

theCube# = CInt(InputBox$("Enter an integer:"))

Print theCube# & " cubed = " & aDouble# & "."
Print bigNum# & " is the biggest cube so far."


more% = MessageBox(msg$, msgSet%)

If more% = IDNO Then
goAgain = FALSE
End If
Wend
Print "All Done."
End Sub

Call KeepGoing
'Output: The user types 3 and selects Yes, then
'4 and selects Yes, then 2 and selects No.
'3 cubed = 27.
'27 is the biggest cube so far.
'4 cubed = 64.
'64 is the biggest cube so far.
'2 cubed = 8.
'64 is the biggest cube so far.
'All Done.

六、文件处理

LotusScript中的三种文件类型:
连续的,最常见,可以通过text编辑器打开。
随机的,默认,用来存储结构化数据,除了通过LotusScript程序,其他不可读。
二进制。最复杂,硬盘上的字符级别的定义文件。

要操作文件里的数据,就要先打开文件。在LotusScript中文件被打开后会有一个在1~255之间的文件号。各种IO操作基本都是使用这个编号而不是文件名。这个编号会保持到文件关闭为止。

常用文件操作:
Close,关闭一个或多个打开的文件,
Get,Input,从文件读取数据,
Kill,删除文件,
Open,打开文件,
Put,Write,向文件写数据。

打开一个连续的文件
Open fileName [For{Input|Output|Append}] As fileNumber [Len = bufferSize][Charset=MIMECharsetName]
Input只读方式打开文件, 文件必须存在,否则报错
Output 只写方式打开文件, 
Append 只写方式打开文件,并且从文件最后开始写入。
以上两种方式文件若是不存在,则新建一个文件。
每次操作单位为一行。
FreeFile函数可以获得没有使用的文件编号。
bufferSize默认为512bytes。
MIMECharsetName指定编码类型,默认情况下使用平台的编码类型。

写入连续文件
可以通过Print#或者Write#语句,向一个通过Output或者Append方式打开的文件,写入变量的值。
String和数字类的数据,都会被转换为String写入文件。
idfile为文件编号
Print #idfile, Var1, Var2

For example:

Dim supV As Variant, tailV As Variant
supV = 456
tailV = NULL
Write #idFile, "Testing", 123, supV, tailV
The statements generate the following line in the file numbered idFile:
"Testing",123,456,#NULL#
Note: True, False, and NULL are stored as strings "#True#", "#False#", and "#NULL#".

读取一个连续文件

可以通过 Line Input #,Input #语句,或者Input函数读取文件的函数到变量中。

Line Input #,根据end-of-line读取一行,并且end-of-line不包含在返回String中。
Do Until EOF(idFile)
Line Input #idFile, iLine
Print iLine
Loop

Input #读取通过Write #语句写入的数据。
如果你发现自己经常用Write # and Input #来处理连续文件。那你可以考虑使用随机文件来处理数据了。随机文件更适合面向记录的数据。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值