问题
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 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
回答2:
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
回答3:
You can create a new Datatype and add all necessary members in it. Then return this new datatype from your function.
回答4:
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 ..
回答5:
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.
回答6:
This will Help you,
you can do it in two methods first one is to pass all values as array for example
function message
message1="Hello How are you?"
message2="i am fine"
message3="Hope you doing fine"
message=Array(message1,message2,message3)
End function
function chat-history
user-history=message
usermessage1=user-history(0)
usermessage2=user-history(1)
usermessage3=user-history(2)
End Function
The Second method is to concatenate and split
function message
message1="Hello How are you?"
message2="i am fine"
message3="Hope you doing fine"
message=message1+"@"+message2+"@"+message3
End function
function chat-history
user-history=message
user-message=split(user-history,"@")
usermessage1=user-message(0)
usermessage2=user-message(1)
usermessage3=user-message(2)
End Function
回答7:
I own nothing. All credit goes to the hardwork of my online friends pradeek and another friend. I hope this helps someone.
'Passing multiple numerical values from one function to another method-1
Function fnFirstFunction()
value1=1
value2=2
value3=3
A=Array(value1,value2,value3)
fnFirstFunction=A
'msgboxA(0)
'msgboxA(1)
End Function
'Belowfunction passes the value returned from the fnFirstFunction and displays
Function fnSecondFunction(ByVal A)
P=A
B=P(0)
MsgBox B
C=P(1)
Msgbox C
D=P(2)
Msgbox D
End Function
A=fnFirstFunction()
'AsfnFirstFunction returns a value, take it to a variable
Call fnSecondFunction(A)'a
'passing multiple character values from one function to another method -2
public function fun1()
msg1="Hello How are you?"
msg2="i am fine"
msg3="Hope you doing fine"
arr1=Array(msg1,msg2,msg3)
fun1=arr1
End function
arr1=fun1()
Call fun2(arr1)
public function fun2(byval arr1)
j=arr1
usermessage1=j(0)
msgbox usermessage1
usermessage2=j(1)
msgbox usermessage2
usermessage3=j(2)
msgbox usermessage3
End Function
'Passing multiple values from one function to another method-3
public function fun1()
msg1="Hello How are you?"
msg2="i am fine"
msg3="Hope you doing fine"
arr1=msg1 + "@" + msg2 + "@" + msg3
fun1=arr1
End function
arr1=fun1()
Call fun2(arr1)
public function fun2(byval arr1)
j=arr1
k= split(j,"@")
usermessage1=k(0)
msgbox usermessage1
usermessage2=k(1)
msgbox usermessage2
usermessage3=k(2)
msgbox usermessage3
End Function
回答8:
By using dictionary Object.This is your function
Function sample_function(ByRef passengerDetails)
''#Some code.................
passengerDetails("passenger")=list(0)
passengerDetails("name")=list(1)
passengerDetails("age")=list(2)
passengerDetails("seatnumber")=list(3)
'#This is an Incomplete function...
end function
'Creating a dictionary object
Set passengerDetails= CreateObject("Scripting.Dictionary")
'calling your function
sample_function(passengerDetails)
Advantage of using dictionary object is, sometime down the line you want the function to return more values(and this function is used by many projects/teams), you have to do is add the values you want to return in the dictionary object, without having to add more parameters(others using this function wont get any error)
回答9:
If you are sure that the list1
array carry only required data in same manner as you posted. Then you can directly pass the array from function too:-
function sample_function(arg1,arg2)
''#Some code.................
passenger = list1(0)
name1 = list1(1)
age1 = list1(2)
seatNumber = list1(3)
sample_function list1
end function
回答10:
Option Explicit
Dim val1, val2
Dim res1, res2, res3, res4
Print "Calling a Sub function"
funcMath1 10, 25, "m"
funcMath1 10, 25, "S"
funcMath1 10, 25, "D"
funcMath1 10, 25, "A"
funcMath1 10, 25, "C"
回答11:
'==============================================
' Date : 09/21/2012
' Created By : Jamil
' VB Script Basic Sub Function and Function Test
'==============================================
Option Explicit
Dim val1, val2
Dim res1, res2, res3, res4
Print "Calling a Sub function"
funcMath1 10, 25, "m"
funcMath1 10, 25, "S"
funcMath1 10, 25, "D"
funcMath1 10, 25, "A"
funcMath1 10, 25, "C"
'(1) simple calculater sub function with no return value
Function funcMath1(val1, val2, opt)
Dim result
Select Case UCase(opt)
Case "M"
result = (val1 * val2)
Print "The result of multiplying is " &val1& " With " &val2& " is " &result
Case "S"
result = (val1 - val2)
Print "The result of subtraction is " &val1& " With " &val2& " is " & result
Case "D"
result = (val1 / val2)
Print "The result of divide is " &val1& " With " &val2& " is " & result
Case "A"
result = (val1 + val2)
Print "The result of Addtion is " &val1& " With " &val2& " is " & result
Case else
msgBox "Your option "& opt &" is invalid!"
End Select
End Function
Print " "
Print "Calling a function"
call funcMath2(10, 25, "M")
call funcMath2(10, 25, "S")
call funcMath2(10, 25, "D")
call funcMath2(10, 25, "A")
call funcMath2(10, 25, "C")
'(2) simple calculater function with return value
Function funcMath2(val1, val2, opt)
Dim result, returnValue
Select Case UCase(opt)
Case "M"
result = (val1 * val2)
Print "The result of multiplying is " &val1& " With " &val2& " is " &result
Case "S"
result = (val1 - val2)
Print "The result of subtraction is " &val1& " With " &val2& " is " & result
Case "D"
result = (val1 / val2)
Print "The result of divide is " &val1& " With " &val2& " is " & result
Case "A"
result = (val1 + val2)
Print "The result of Addtion is " &val1& " With " &val2& " is " & result
Case else
msgBox "Your option "& opt &" is invalid!"
End Select
funcMath2 = result
End Function
来源:https://stackoverflow.com/questions/3403581/qtp-how-can-i-return-multiple-values-from-a-function