All Forums >> [Scripting] >> WSH & Client Side VBScript >> Can a vb Function return 2 values? Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
i'm writing a script, (actually a function in VB.net), and I'd like it to return 2 separate values. (without making a Global variable). Can a function return an array of results?
The function generates 2 values, but i can only return 1 to the caller. Is this possible?
(I hope this makes sense to someone), or I can eloborate or post code.
You probably won't get actual code here because you say you are doing this in VB.Net and this is a VBScript site. The technologies are soooo far apart from each other. Having said that, it is entirely possible to return an array from a function (subs don't return anything) in VBScript. I would be amazed to find that it is not possible in VB.Net as well.
Let me post some sample code (translated into vbscript) to demonstrate what I"m trying to accomplish
Below is a simplified version of what I want (written in vbsript).
Running the following code will produce a msgbox with "3", but what i really want the funtion to return is the value 3 and 1. (without making them global variables)
Return = Results("joe") MsgBox Return 'end
Function Results(UserName) if UserName = "joe" then a = 3 b = 1 else a = 1 b = 0 End If Results = a End Function
I often use this method to retrun an error number and an error description from a function. Although I think I will start using the built in Err object to handle this in the future.
What is the syntax for that when you want to return an array that has been already been populated? I was trying to do this the other day and couldn't get it working. If you're using a bigger array than two elements then you're going to want a different way to return the array. I've changed the code to show what I tried, but this doesn't work.
Couple of things. First, don't use Array for the name of an array. It is a reserved word. Second, there is a logic issue with the code that you posted. If the user name that is passed in is "joe", then the code to populate the array will never be executed so you are not passing back an array at all. Third, use Option Explicit. It will make your life easier in the future. Fourth, I sugest using a variable naming convention that indicates what type of data the variable will hold. This is not strictly needed, but I think it makes code easier to read. Here is your code with these modifications.
ORIGINAL: ebgreen Couple of things. First, don't use Array for the name of an array. It is a reserved word. Second, there is a logic issue with the code that you posted. If the user name that is passed in is "joe", then the code to populate the array will never be executed so you are not passing back an array at all. Third, use Option Explicit. It will make your life easier in the future. Fourth, I sugest using a variable naming convention that indicates what type of data the variable will hold. This is not strictly needed, but I think it makes code easier to read. Here is your code with these modifications.
Another way of doing this is to join the array into a string and return that, using some character as a seperator such a VbCrLf.
_____________________________
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
"It is better to die like a tiger, than to live like a pussy." -Master Wong, from Balls of Fury
Thanks. My problem was stupidity rather than syntax then. I typed that code without thinking too much about what I was writing. But even in the real world case that I was trying to solve I'd stuffed it up too.
When I couldn't figure it out the other day, I used a different solution anyway. Which I'll relay here in case anyone else wants to know.
If you use a Sub instead of a Function, you can end up with the exact same result, but of course using a different technique. When using a Sub, it relys on the fact that when you pass an array to the Sub, you're not passing the actual array, just a reference to the array. So when you manipulate an array in the sub, you actual are manipulating the array you started with.
E.G. Option Explicit Dim arrReturn Call Results(arrReturn, "joe") MsgBox arrReturn(0) & " <--> " & arrReturn(1) 'End
Sub Results(arrFinal, strUserName) arrFinal = Array(1, 0) If strUserName = "joe" Then arrFinal(0) = 3 arrFinal(1) = 0 End If End Sub
You'll notice that there is no return statement like in a Function, because arrFinal is just a reference to the very same array used in the main part of the script, arrReturn. So that's the code I used to get around the problem. But it's good to know the Function works as well.
clloyd - This can be controlled by using ByRef or ByVal argument in a function statement.
Script56.CHM:
The arglist argument has the following syntax and parts: [ByVal | ByRef] varname[( )] Arguments
ByVal Indicates that the argument is passed by value. ByRef Indicates that the argument is passed by reference. varname Name of the variable representing the argument; follows standard variable naming conventions.