[Gambas-user] Returning multiple values from a function

Benoit Minisini gambas at ...1...
Thu Apr 3 11:59:38 CEST 2008


On jeudi 3 avril 2008, Rolf-Werner Eilert wrote:
> Benoit Minisini schrieb:
> > On jeudi 3 avril 2008, Rolf-Werner Eilert wrote:
> >> This is just the way I do it. But when I have more than 2 values to
> >> separate, I'll use another string[] to have them splitted (though I like
> >> to use a Tab (chr$(9)). This runs incredibly fast in Gambas.
> >>
> >> Let's assume you receive a string like "10|cat|male|black" I would do
> >>
> >> dim partstring as new string[]
> >>
> >> partstring = split(sResult, "|")
> >>
> >> 'and now you can examine the part-strings:
> >>
> >> if val(partstring[0]) = 10 then...
> >>
> >> if partstring[1] = "cat" then...
> >>
> >> Rolf
> >>
> >> Jaap Cramer schrieb:
> >>> When I need multiple returnvalues I use an array
> >>> Mostly of the string[] type.
> >>> When it contains different things, like an integer and a string, I put
> >>> them in 1 string, and split them
> >>>
> >>> sResult = DoSomething()   ' call the function
> >>> result is "10|cat"               ' i use a pipe to divide my string
> >>>
> >>> sInteger = val(left(sResult, instr(sResult, "|")-1))
> >>> sString = mid(sResult, instr(sResult, "|")+1)
> >>>
> >>> I think this should work...
> >>>
> >>> ----------------------------------------
> >>>
> >>>> From: rterry at ...1822...
> >>>> To: gambas-user at lists.sourceforge.net
> >>>> Date: Thu, 3 Apr 2008 13:37:48 +1100
> >>>> Subject: [Gambas-user] Returning  multiple values from a function
> >>>>
> >>>> Hope this is not too ambiguous or dumb:
> >>>>
> >>>> Is it possible to return more than 1 value from a function?
> >>>>
> >>>>
> >>>> This works:
> >>>>
> >>>> function  DoSomething(name as string) as integer[]
> >>>>  Dim numbers as integer[2]
> >>>>
> >>>> 'do whatever in here
> >>>> numbers[0]=10
> >>>> numbers[1] = 20
> >>>> return numbers
> >>>>
> >>>>
> >>>> however seems a bit clumbsy
> >>>>
> >>>> doing this:
> >>>>
> >>>> function  Dosomething(name as string) as integer,string
> >>>>
> >>>> REturn   10,"cat"
> >>>>
> >>>> Dosn't
> >>>>
> >>>> Probably a stupid question. I guess if you want back non identical
> >>>> types one would just use a variant array?
> >>>>
> >>>>
> >>>>
> >>>> thanks
> >>>>
> >>>> Richard
> >
> > I think the better is using an array of Variant, or better an anoynmous
> > array:
> >
> > FUNCTION DoSomething(Name AS String) AS Array
> >
> >   DIM FirstResult AS ...
> >   DIM SecondResult AS ...
> >
> >   ' Do something
> >
> >   RETURN [FirstResult, SecondResult]
> >
> > END
>
> Very interesting idea, but if I have more than a few result lines, how
> could I spare me writing
>
> RETURN [FirstResult, SecondResult, ThirdResult, ForthResult ... ]
>
> Rolf
>

[...] can take up to 63 elements.

-- 
Benoit Minisini




More information about the User mailing list