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?
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
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
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With