我正在尝试编写一个函数,该函数可以从具有2个参数的Function返回多个值。
例如:
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
这个名为sample_function的函数有2个参数,名为arg1,arg2。当我在我的驱动程序脚本中调用此函数时,如value = sample_function(2,Name_person),此函数应该返回乘客,name1,age1,seatNumber值。
我怎样才能实现这个目标?
EDIT(LB): QTP使用VBScript来指定测试例程,因此我将其重命名为VBScript,VB,因为解决方案可能在VBScript中。
答案 0 :(得分:23)
一个简单的解决方案是返回一个数组:
function foo()
foo=array("Hello","World")
end function
x=foo()
MsgBox x(0)
MsgBox x(1)
如果您碰巧经常使用同一批值,那么可能需要付费才能使其成为用户定义的类:
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
答案 1 :(得分:3)
在VBScript中,所有函数参数都是输入/输出参数(也称为ByRef参数)。因此,您只需使用函数参数即可返回数据。例如:
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
答案 2 :(得分:1)
您可以创建新的数据类型并在其中添加所有必需的成员。然后从函数中返回这个新数据类型。
答案 3 :(得分:1)
您可以通过从函数返回字典对象来完成。
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
这里在字典中我已经添加到名为Msg1和Msg2的键中,类似地我们可以添加更多具有不同类型值的键,如int,Boolean,array任何类型的数据..
答案 4 :(得分:1)
声明你的功能。
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
然后当你调用它时只输入你想要返回的变量。
答案 5 :(得分:0)
这会帮助你,
你可以用两种方法做到第一种方法是将所有值作为数组传递
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
第二种方法是连接和拆分
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
答案 6 :(得分:0)
'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
答案 7 :(得分:0)
使用字典对象。这是你的功能
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)
使用字典对象的好处是,有时你希望函数返回更多的值(这个函数被许多项目/团队使用),你要做的就是在字典中添加你想要返回的值对象,无需添加更多参数(使用此函数的其他参数不会出现任何错误)
答案 8 :(得分:0)
如果您确定list1
数组仅以您发布的相同方式携带所需数据。然后你也可以直接从函数传递数组: -
function sample_function(arg1,arg2)
''#Some code.................
passenger = list1(0)
name1 = list1(1)
age1 = list1(2)
seatNumber = list1(3)
sample_function list1
end function
答案 9 :(得分:-1)
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"
答案 10 :(得分:-2)
'==============================================
' 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