我想从 LotusScript 中的函数返回一个列表。
例如。
Function myfunc() List As Variant
Dim mylist List As Variant
mylist("one") = 1
mylist("two") = "2"
myfunc = mylist
End Function
Dim mylist List As Variant
mylist = myfunc()
这可能吗?
如果是这样,正确的语法是什么?
我想从 LotusScript 中的函数返回一个列表。
例如。
Function myfunc() List As Variant
Dim mylist List As Variant
mylist("one") = 1
mylist("two") = "2"
myfunc = mylist
End Function
Dim mylist List As Variant
mylist = myfunc()
这可能吗?
如果是这样,正确的语法是什么?
似乎您无法从函数返回列表。
您可以轻松地将其包装在一个类中并返回该类。
例如。
Class WrappedList
Public list List As Variant
End Class
Function myfunc() As WrappedList
Dim mylist As New WrappedList
mylist.list("one") = 1
mylist.list("two") = "2"
Set myfunc = mylist
End Function
在这里找到了答案:LotusScript 的列表错误再次来袭
这对我来说很好。我已将一个值设置为字符串,将另一个值设置为整数,这样您就可以看到变体的行为本身。
Sub Initialize
Dim mylist List As Variant
Call myfunc(mylist)
Msgbox "un = " + mylist("one")
Msgbox "deux = " + cstr(mylist("two"))
End Sub
Sub myfunc(mylist List As Variant)
mylist("one") = "1"
mylist("two") = 2
End Sub
您可以获得一个返回列表的函数,只需删除函数中的“列表”位,而不是
Function myfunc() List As Variant
...
End Function
...做:
Function myfunc() As Variant
然后你可以像你已经做的那样调用你的函数。
Dim mylist List As Variant
mylist = myfunc()
列表很棒,我一直在使用它们,但我发现列表有一个限制......
如果如果有这样的功能:
Public Function returnMyList() as Variant
Dim myList List as Variant
Dim s_test as String
Dim i as Integer
Dim doc as NotesDocuemnt
Dim anotherList List as String
'// ...set your variables however you like
myList( "Some Text" ) = s_text
myList( "Some Integer" ) = i
myList( "Some Doc" ) = doc
'// If you returned the list here, you're fine, but if you add
'// another list...
anotherList( "one" ) = ""
anotherList( "two" ) = ""
myList( "Another List" ) = anotherList
'// This will cause an error
returnMyList = myList
'// I bodge around this by writting directly to a List
'// that is set as global variable.
End Function
简单地说,你必须有一个返回变体的函数。我可以看到您喜欢以面向对象的方式进行操作,但是如果您只想通过程序“完成”是最简单的。
虽然有几种方法可以做到这一点,但这是我的首选方式。请注意,您可以创建任何原始数据类型的列表(即字符串、变体、整数、长整数等)。
Function myfunc as variant
dim mylist list as variant
mylist("somename") = "the value you want to store"
mylist("someothername") = "another value"
myfunc = mylist
End Function
使用 myfunc ..
sub initialise
dim anotherlist list as variant
anotherlist = myfunc
end sub
如果需要,您可以通过简单地以这种方式定义 myfunc 来向 myfunc 添加参数
function myfunc(val1 as variant, val2 as variant) as variant
你用像这样的参数以同样的方式调用它
anotherlist = myfunc("a value", "another value")
请注意,“变体”是您的通用数据类型。重要的是 myfunc 作为变体是您可以从函数返回列表和变体的唯一方法。