如何在LotusScript函数中设置文档参数



考虑下面的示例。preferencesDoc作为零传入。它在该函数中的赋值被忽略,并且在集合之后什么都不保留。tmpDoc设置良好。两个分配是相同的,所以这不是视图问题。preferencesDoc的分配被阻止,显然是因为它是一个参数。没有错误,按键查找工作正常,tmpDoc的成功分配证明了这一点。

Function test(preferencesDoc As NotesDocument) 
    If preferencesDoc Is Nothing then
        Set preferencesDoc=docLookupView.getDocumentByKey("GENERAL_PREFERENCES", True)
    End if
    Dim tmpDoc As NotesDocument
    Set tmpDoc=docLookupView.getDocumentByKey("GENERAL_PREFERENCES", True)
End Function 

有人能解释一下这里发生了什么以及如何进行吗?

澄清

很高兴看到人们不断提出想法。然而,你必须意识到,这里的这个功能只是为了说明我的问题。这是一个帮助我沟通问题的基本方法,而不是我真正代码的一部分。请继续提问。

同样,如果preferencesDoc是作为零传入的,那么它在函数中的"修复"赋值将被完全忽略。托德似乎有什么发现。当我传入已设置的preferenceDoc时,我可以将其重新分配给其他文档。

回答

call test(Nothing) // will not work
---
Dim doc as NotesDocument
call test(doc) // will work

Tode的关键语句:如果将"Nothing"作为参数传递,则它将保持为零。如果您传递一个未初始化的NotesDocument,那么它将被初始化。

托德和克努特都切中要害,我认为里奇也在暗示同样的事情。谢谢我相信克努特是第一个,所以我要表扬他。

这些年来,我一直在Notes中编码,这是我第一次遇到这个问题。每天都在学习。:(

您的代码确实有效。只需使用Call test(doc)调用您的函数即可。您可以使用进行测试

Dim doc As NotesDocument
Call test(doc)
If doc Is Nothing Then
    Print "Nothing"
Else
    Print doc.form(0)
End If 

获取偏好文档的一种更舒适的方法是不使用参数:

Function GeneralPreferences() As NotesDocument
    Static preferencesDoc As NotesDocument
    If preferencesDoc Is Nothing Then
        ' ... get your docLookupView
        Set preferencesDoc=docLookupView.getDocumentByKey("GENERAL_PREFERENCES", True)
    End If
    Set GeneralPreferences = preferencesDoc
End Function

然后你就可以处理这样的结果了

Print GeneralPreferences.form(0)
Print GeneralPreferences.created

并且您不需要声明额外的NotesDocument。使用preferencesDoc的Static,文档只从数据库中读取一次-它被"缓存"在函数

这在LotusScript中是正常的。如果传入"nothing",则这不是NotesDocument类型的对象,而只是"nothing"。。。没有什么是不能赋值的。

但是你已经做了正确的事情:使用一个函数。

你这样调用函数:

Set preferenceDoc = test(preferenceDoc) 

是正确的。但是你忘了交回文件。你的功能应该是这样的:

Function test(preferenceDoc as NotesDocument) As NotesDocument
  Dim docTemp as NoresDocument
  If preferenceDoc is Nothing then
    Set docTemp = docLkpView.GetDocumentBykey( "GENERAL_PREFERENCES", True )
  Else
    Set docTemp = preferenceDoc
  End If
  ' here comes the "magic"
  Set test=docTemp
End Function

当然,您可以完全去掉docTemp,只需用相应行中的函数名替换docTemp即可,然后就不需要最后一行了。。。

对象是通过引用传递的,所以您所描述的行为看起来肯定很奇怪。但就风格而言,我无论如何都不建议这样做。索德效应模糊了代码的逻辑。您的函数应该声明为返回NotesDocument,并且应该通过调用

Set doc = test(doc)

最新更新