Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Can a vb Function return 2 values?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,31380
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
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!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Can a vb Function return 2 values? - 2/21/2006 8:35:30 AM   
  edavis6678

 

Posts: 122
Score: 0
Joined: 1/12/2006
Status: offline
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.
 
 
Post #: 1
 
 RE: Can a vb Function return 2 values? - 2/21/2006 8:43:03 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
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.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to edavis6678)
 
 
Post #: 2
 
 RE: Can a vb Function return 2 values? - 2/21/2006 9:37:26 AM   
  edavis6678

 

Posts: 122
Score: 0
Joined: 1/12/2006
Status: offline
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

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Can a vb Function return 2 values? - 2/21/2006 9:42:40 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
Return =  Results("joe")
MsgBox Return(0) & " <--> " & Return(1)
'end

Function Results(UserName)
  if UserName = "joe" then
      a = 3
      b = 1
  else
      a = 1
      b = 0
  End If
  Results = Array(a, b)
End Function

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to edavis6678)
 
 
Post #: 4
 
 RE: Can a vb Function return 2 values? - 2/21/2006 10:12:51 AM   
  edavis6678

 

Posts: 122
Score: 0
Joined: 1/12/2006
Status: offline
thank you!, exactly what I was looking for.

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Can a vb Function return 2 values? - 2/22/2006 2:06:42 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
This is good to know, never thought about it before.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to edavis6678)
 
 
Post #: 6
 
 RE: Can a vb Function return 2 values? - 2/22/2006 2:11:35 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
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.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to mbouchard)
 
 
Post #: 7
 
 RE: Can a vb Function return 2 values? - 2/22/2006 6:04:07 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
so you can only return one item doesnt matter if the item can hold multiple things, ingenious!

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: Can a vb Function return 2 values? - 2/22/2006 8:33:57 PM   
  clloyd

 

Posts: 6
Score: 0
Joined: 2/22/2006
Status: offline
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.

Anyone know?

Return =  Results("joe")
MsgBox Return(0) & " <--> " & Return(1)
'end

Function Results(UserName)
  if UserName = "joe" then
      a = 3
      b = 1
  else
      Array(0) = 1
      Array(1) = 0
  End If
 
Results = Array
End Function

(in reply to kirrilian)
 
 
Post #: 9
 
 RE: Can a vb Function return 2 values? - 2/23/2006 12:57:30 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
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.

      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to clloyd)
 
 
Post #: 10
 
 RE: Can a vb Function return 2 values? - 2/23/2006 4:53:52 AM   
  DiGiTAL.SkReAM


Posts: 1184
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

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

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: Can a vb Function return 2 values? - 2/23/2006 10:44:10 AM   
  clloyd

 

Posts: 6
Score: 0
Joined: 2/22/2006
Status: offline
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.

(in reply to edavis6678)
 
 
Post #: 12
 
 RE: Can a vb Function return 2 values? - 1/7/2007 11:14:00 AM   
  mrmore

 

Posts: 5
Score: 0
Joined: 11/9/2006
Status: offline
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.

(in reply to clloyd)
 
 
Post #: 13
 
 RE: Can a vb Function return 2 values? - 1/8/2007 7:08:53 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Code:


      

and output:


      

to demonstrate mrmore's citation (and to emphasize the importance of the
"pass me per value" parantheses)

(in reply to mrmore)
 
 
Post #: 14
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Can a vb Function return 2 values? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts