QTP:如何从函数返回多个值

发布于 2024-09-12 14:11:43 字数 565 浏览 3 评论 0原文

我正在尝试编写一个函数,它可以从具有 2 个参数的函数返回多个值。

例如:

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))时,此函数应返回乘客、姓名 1、年龄 1、座位号值。

我怎样才能做到这一点?

编辑(LB):QTP使用VBScript来指定测试例程,所以我将其重新标记为VBScript,VB,因为解决方案可能在VBScript中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

死开点丶别碍眼 2024-09-19 14:11:43

一个简单的解决方案是返回一个数组:

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

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
想挽留 2024-09-19 14:11:43

在 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

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
臻嫒无言 2024-09-19 14:11:43

您可以创建一个新的数据类型并在其中添加所有必需的成员。然后从您的函数返回这个新的数据类型。

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

筱武穆 2024-09-19 14:11:43

您可以通过从函数返回字典对象来完成此操作。

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 任何类型的数据..

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 ..

葬心 2024-09-19 14:11:43

像这样声明你的函数。

函数示例(arg1、arg2、乘客、姓名、年龄1、座位号)
''#一些代码......
乘客=列表1(0)
名称 1 = 列表 1(1)
年龄1 = 列表1(2)
座位号 = 列表 1(3)
''#这是一个不完整的函数...
结束函数示例

然后当你调用它时只需输入你想要返回的变量。

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.

把人绕傻吧 2024-09-19 14:11:43

这将对您有所帮助,

您可以通过两种方法完成此操作,第一种是将所有值作为数组传递,例如

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

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
剩一世无双 2024-09-19 14:11:43

我一无所有。所有功劳都归功于我的网上朋友普拉迪克和另一位朋友的辛勤工作。我希望这对某人有帮助。

'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

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
野の 2024-09-19 14:11:43

通过使用字典对象。这是你的函数

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)

使用字典对象的优点是,有时你希望函数返回更多值(并且这个函数被许多项目/团队使用),你要做的就是添加你想要的值想要返回字典对象,而不必添加更多参数(其他使用此函数的人不会得到任何错误)

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)

蹲在坟头点根烟 2024-09-19 14:11:43

如果您确定 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 

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 
微暖i 2024-09-19 14:11:43
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"
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"
铁憨憨 2024-09-19 14:11:43
'==============================================
' 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
'==============================================
' 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文