我想从LotusScript中的函数返回一个List。
例如
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()
这可能吗?
如果是,那么正确的语法是什么?
答案 0 :(得分:8)
您似乎无法从函数返回List。
您可以轻松地将其包装在课堂中并返回课程。
例如
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
答案 1 :(得分:2)
这对我来说很好。我已将一个值设置为字符串,另一个值设置为整数,以便您可以看到变体的行为本身。
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
答案 2 :(得分:1)
你可以得到一个函数toi返回一个列表,只是去除你函数中的“List”位,所以代替
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
答案 3 :(得分:1)
简单地说,你必须有一个返回变体的函数。我可以看到你喜欢以面向对象的方式做这件事,但如果你只是想“完成它”,那么程序化是最简单的。
虽然有几种方法可以做到这一点,但这是我的首选方式。请注意,您可以创建任何基本数据类型的列表(即字符串,变量,整数,长等)。
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")
请注意,“variant”是您的通用数据类型。重要的是myfunc作为变体是从函数返回列表和变体的唯一方法。