Can I return a List from a LotusScript Function?
Asked Answered
C

4

5

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?

Cuneate answered 27/2, 2009 at 4:33 Comment(1)
This is one example of how Lotus Notes/Domino kills you with a thousand paper cuts....Decosta
C
8

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

Cuneate answered 27/2, 2009 at 5:16 Comment(1)
Good answer! +1 but.. Ugh, I hate variants! You could define the class as having Public list List As NotesDocument (for example - could be any class / type) . That way when you do a foreach x in wrappedList.list .. End For, on the list, the index variable will have a type. Otherwise if you leave it as variant, you vget no typeahead from Notes Designer because it's a variantRepel
B
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
Beebread answered 27/2, 2009 at 4:50 Comment(0)
A
2

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
Abaca answered 27/5, 2009 at 7:23 Comment(1)
The root of the problem when having a List contained in a List is that the Variant type cannot hold this kind of object. It might come as a surprise that a Variant can't hold a certain type, but this limitation has literally cost me hundreds of hours once..Pick
H
1

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.

Heidi answered 30/12, 2009 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.