Can I return a List from a LotusScript Function?

牧云@^-^@ 提交于 2020-01-01 11:33:42

问题


I want to return a List from a Function in LotusScript.

eg.

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()

Is this possible?

If so, what is the correct syntax?


回答1:


It seems you can't return a List from a Function.

You can easily wrap it in a class though and return the class.

eg.

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

Answer was found here: LotusScript's List bug strikes again




回答2:


This works fine for me. I've set one value to string and the other to integer so you can see that the variants behave themselves.

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



回答3:


Simply put you gotta have a function that returns a variant. I can see that you like to do it in an object oriented fashion, but if you just want to "get it done" procedurally is the easiest.

Although there are a couple of ways to do this, this is my preferred way. Note that you can create a list of any primitive data type, (ie string, variant, integer, long etc).

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

To use myfunc ..

sub initialise
    dim anotherlist list as variant
    anotherlist = myfunc
end sub

You can add parameters to myfunc if you need to by simply defining myfunc this way

function myfunc(val1 as variant, val2 as variant) as variant

You call it the same ways with parameters like this

anotherlist = myfunc("a value", "another value")

Note that "variant" is your universal datatype. What's important is that myfunc as a variant is the only way you can return lists and variants from a function.




回答4:


You can get a function toi return a list, just get rid of "List" bit in your function, so instead of

Function myfunc() List As Variant
   ...
End Function

...do:

Function myfunc() As Variant

then you can call your function as you already do.

Dim mylist List As Variant
mylist = myfunc()

Lists are great and I use them all the time, but I have found one limitation with lists...

if if have a function like:

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


来源:https://stackoverflow.com/questions/593533/can-i-return-a-list-from-a-lotusscript-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!