Thanks:  0
Likes:  0
Dislikes:  0

# Thread: How to return more than 1 value in functions

1. ## How to return more than 1 value in functions

Dude,
I would like to know how to return more than one value for one function. I seen some where in the forum post. I forgot that forum topic. I request you to help me out from my questions.

Regards,
Ram.

2. ## Re: How to return more than 1 value in functions

Don't know if its the best way but I use an array.

ie.

Function CheckScreen

x = ABC
y = CDE

CheckScreen = Array (x, y)

End Function

Obviously you have to know the position of each value although this could be achive perhaps by adding before the Function within the .vbs file something like;

xPostion = 0
yPosition = 1

Then within the code which calls the function you could have;

z = CheckScreen()

xValue = z(xPosition)
yValue = z(yPosition)

Apologies that this relies heavliy on my COBOL background!!!

There probably is a smarter way of doing it.

3. ## Re: How to return more than 1 value in functions

a=2
Call TwoValues(a,b,c) ' a=input, c&amp;b=return values

MsgBox b
MsgBox c

Function TwoValues(a,b,c)
b = a * a
c=b+a
End Function

Mark Smith.

4. ## Re: How to return more than 1 value in functions

Dude,
Is below code make sense?

strA=10
strB=20
strResult1=strA+strB
strResult2=strB-strA
End Function
---------------------
Msgbox strOutput
------------------------
It will return result as 30,10

From that we should split and take the values.

I hope it will make sense as per knowledge.

Let me know if not,

Regards,
Ram.

5. ## Re: How to return more than 1 value in functions

Yeah it would work but an array does exactly the the same thing but without the added problem of a delimiter.

Using a delimiter causes issues such as what if you want to pass the delimiter it self as a value rather than a delimiter? What if you want to add further values?

An array achives exactly the same and more on top. If you need to change it to delimited string, there is a built in VBS function to do that afterwards anyway.

strA=10
strB=20
strResult1=strA+strB
strResult2=strB-strA
End Function
---------------------
Msgbox (Split(strOutput,","))
------------------------
It will return result as 30,10

If you've never used arrays, I would definitely advise you look it up on the QTP help guide and on the internet.

As you get more confident you can even use multi dimentional arrays to allow even more flexibilty

6. ## Re: How to return more than 1 value in functions

[ QUOTE ]
Dude,
Is below code make sense?

[/ QUOTE ]

You need to pass the arguments Call X(arg1,arg2)

Function X(arg1,arg2)
...
End Function

7. ## Re: How to return more than 1 value in functions

Thanks a lot dude. I am good enough in Array. Let me look in to that part here after.

Regards,
Ram.

8. ## Re: How to return more than 1 value in functions

[ QUOTE ]
Yeah it would work but an array does exactly the the same thing but without the added problem of a delimiter.

[/ QUOTE ]

What about passing an object to a function Robbie?

Mark Smith.

9. ## Re: How to return more than 1 value in functions

I've not got around to learning about objects yet!

How would I do that? (you many need to use layman terms [img]/images/graemlins/wink.gif[/img])

10. ## Re: How to return more than 1 value in functions

Something like this Robbie, not sure an object can be passed in an array (may be wrong but I've never seen an example).

PropsCount = Props.Count
For i = 0 To PropsCount-1
PropName = Props(i).Name
PropValue = Props(i).Value
MsgBox PropName &amp; " = " &amp; PropValue
Next

End Function

Hey any chance you can test whether it's possible?

Mark.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.