0

考虑下面的例子。optionsDoc 作为空值传入。它在此函数中的分配被忽略,并且在设置后它仍然没有任何内容。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 中编码,这是我第一次遇到这个问题。每天学习一些东西。:)

4

3 回答 3

1

这在 LotusScript 中是正常的。如果您传入“nothing”,那么这不是 NotesDocument 类型的对象,而只是“Nothing”......并且无法为 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 ,那么你不需要最后一行......

于 2013-05-28T04:51:52.413 回答
1

您的代码确实有效。只需调用你的函数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文档仅从数据库中读取一次-它在函数中“缓存”

于 2013-05-28T05:30:45.927 回答
0

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

Set doc = test(doc)
于 2013-05-28T04:39:53.763 回答