tags:

views:

84

answers:

3

Hi ,

I'm trying to write a function which can return multiple values from a Function which is having 2 arguments.

eg:

function sample_function(arg1,arg2)
    ''#Some code.................

    passenger = list1(0)
    name1 = list1(1)
    age1 = list1(2)
    seatNumber = list1(3)
    ''#This is an Incomplete function...    
end function sample_function

Here this function named sample_function has 2 argument named arg1, arg2. When i call this function in my Driver Script like value = sample_function(2,Name_person), this function should return me passenger, name1,age1,seatNumber values.

How can i achieve this?

EDIT (LB): QTP uses VBScript to specify the test routines, so I retagged this to VBScript, VB because the solution is probably within VBScript.

+1  A: 

You can create a new Datatype and add all necessary members in it. Then return this new datatype from your function.

schoetbi
+7  A: 

A straightforward solution would be to return an array:

function foo()
 foo=array("Hello","World")
end function

x=foo()

MsgBox x(0)
MsgBox x(1)

If you happen to use the same batch of values more often than once, then it might pay to make it a user defined class:

class User
 public name
 public seat
end class 

function bar()
 dim r 
 set r = new User
 r.name="J.R.User"
 r.seat="10"
 set bar=r
end function

set x=bar()

MsgBox x.name
MsgBox x.seat
Luther Blissett
+1  A: 

In VBScript, all function parameters are input/output parameters (also called ByRef parameters). So you could return your data simply using the function parameters. Example:

Function Test(a, b, c)
   a = 1
   b = 2
   c = 3
End Function

Test x, y, z
MsgBox x  ' Shows 1
MsgBox y  ' Shows 2
MsgBox z  ' Shows 3
fmunkert
@Fmunkert: i agree that this will also work, But this will not work when we have 2 arguments and there are 3 return types. @Luther: Thank u , this worked for me.....
TestGeeK