QTP: How can I return multiple Values from a Function

70,572

Solution 1

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

Solution 2

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

Solution 3

You can do it by returning dictionary object from the function .

set dictFunRetun=foo()
msgbox dictFunRetun.item("Msg1")
msgbox dictFunRetun.item("Msg2")
function foo()
    set fnReturn=CreateObject("Scripting.Dictionary")
    fnReturn.Add "Msg1","First Variable"
    fnReturn.Add "Msg2","Second Variable"
    set foo=fnReturn
end function

here in dictionary i have added to keys named Msg1 and Msg2 similarly we can add more keys with having value of different types like int, Boolean ,array any type of data ..

Solution 4

declare your function like this.

function sample (arg1, arg2, passenger, name, age1, seatNumber)
''#Some code.................
passenger = list1(0)
name1 = list1(1)
age1 = list1(2)
seatNumber = list1(3)
''#This is an Incomplete function...
end function sample

then when you call it just input the variables you want to return.

Share:
70,572
Sam
Author by

Sam

Experience in building framework using testing tools like Cypress, selenium, webdriver, UFT/QTP. API testing framework using SOAP UI, POSTMAN, REST-Assured.

Updated on July 21, 2022

Comments

  • Sam
    Sam almost 2 years

    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.

  • Ekkehard.Horner
    Ekkehard.Horner almost 12 years
    As the oxymeron "sub function" indicates and the use of Call to ignore return values proves, this code snippet is a dangerous example of how not to program in VBScript. Furthermore it does not address the question of returning multiple values from a function.
  • TheBlastOne
    TheBlastOne almost 11 years
    What result? The functions return no result at all. So call cannot be wrong in this case ;-)
  • Ben
    Ben almost 11 years
    Hi, your post has been flagged as "low quality", probably because it consists solely of code. You could massively improve your answer by providing an explanation of exactly how and why this answers the question?