How do I return an array of variants from LS function?

I’ve searched this forum and found several entries on how to return an array in a Lotuscript function. In my case, I want to return an array of variants. Here is my code:

Function getValues( strRole As String, strBus As String ) As Variant

Dim varPersons As Variant

Dim idPersons As Variant

Dim macro0 As String, macro1 As String

Dim varFinal( 0 To 1 ) As Variant



If ( strRole = "Business" ) Then

	macro0 = |@DbLookup("" : ""; "" : ""; "DJSbus"; "| + strBus + |"; "List_Approver" )|

	macro1 = |@DbLookup("" : ""; "" : ""; "DJSbus"; "| + strBus + |"; "List_ID" )|

Else

	macro0 = |@DbLookup("Notes" : "NoCache"; "" : ""; "DJSReviewers"; "| + strRole + |"; "List_Reviewer" )|

	macro1 = |@DbLookup("Notes" : "NoCache"; "" : ""; "DJSReviewers"; "| + strRole + |"; "List_ID" )|

End If

	

varFinal( 0 ) = Evaluate( macro0 )

varFinal( 1 ) = Evaluate( macro1 )



getValues = varFinal   <---- Type mismatch

End Function

This is a test agent I’m using right now. The code works fine - in other words, don’t try to decipher the DB lookups, etc.

This function will always return an array that contains 2 variants. I may even change the DB Lookups, but my point is I want to be able to return an ARRAY of VARIANTS.

Please help. Thank you!

Debbie

Subject: How do I return an array of variants from LS function?

if I understood you correctly, perhaps this will help, it returns the list.

 Dim UnrestrictedList (1 To 10) As Variant

 Dim i As Integer

     

 Set db = s.currentdatabase

 Set view = db.getview ("Server\Servers")

 Set doc = view.getfirstdocument

 

i = 0

Forall v In doc.getitemvalue("UnrestrictedList")

'Messagebox (v)

i = i + 1

UnrestrictedList (i) = v

 'Messagebox UnrestrictedList(i)

 End Forall

Subject: RE: How do I return an array of variants from LS function?

The code you entered is similar to mine, but nowhere are you setting the array of variants to a variant. I appreciate your input, but if you get a variant assigned to an array of variants without getting a type mismatch error, let me know.

Thank you.

Subject: RE: How do I return an array of variants from LS function?

You can’t set a variant to an array of variants. IMHO, this is to restrict complexity and maintain a consistent syntax.

I think that you have to move away from a Function based approach and set the variant array via a Sub call. For this to work, the array itself should be changed to a List type unless you have a fixed size variant array.

e.g.

Sub adjustList(myList List As Variant)

. . Dim v As Variant

. . v = Evaluate(“@UserRoles”)

. . myList(“Roles”) = v

. . v = Evaluate(“@UserName”)

. . myList(“Name”) = v

End Sub

and called like …

Sub Initialize

. . Dim x List As Variant

. . Dim v As Variant

. . Call adjustList(x)

. .

. . v = x(“Roles”)

. . Forall z In v

. . . . msg = msg + " - " + Cstr(z)

. . End Forall

. . v = x(“Name”)

. . Forall z In v

. . . . msg = msg + " - " + Cstr(z)

. . End Forall

. . Msgbox msg

End Sub

Subject: RE: How do I return an array of variants from LS function?

Hi Andrew, THANK YOU! I changed my function to a subroutine like you suggested and let the sub “set” the list and then I use the values in my main routine.

Works great and I appreciate the input. Seems to be the closest to what I wanted anyway. Instead of referencing aryBlah( 0 ) and aryBlah( 1 ), I reference lstBlah( “names” ) and lstBlah( “id” ).

Thank you!

Debbie

Subject: RE: How do I return an array of variants from LS function?

There doesn’t seem to be a way to do this. Variables declared explicitly as arrays just cannot be copied with an assignment statement. If the variable were of type Variant and happened to contain an array, it could be copied, but arrays whose elements are arrays are always problematic, regardless of their declared types.

If you’re passing around data items with a complex structure, look into using custom types and classes, especially classes. You can only go so far with arrays.

(FYI: One way I can think of to create the array of arrays and store it in a variant, is something like:

Dim tmp as variant

tmp = Evaluate(" @Today : @Today ")

tmp(0) = Evaluate(“…”) ’ your @DbLookup…

etc.