|
-
Aug 16th, 2006, 06:29 PM
#1
Thread Starter
Frenzied Member
[2005] return 2 values from a function?
how do u make it so instead of returning one value from a function u can return 2?
-
Aug 16th, 2006, 06:44 PM
#2
Fanatic Member
Re: [2005] return 2 values from a function?
You have to use an output parameter, you cannot return two values the traditional way.
Public Function MyFunction(ByRef returnValue2 As Double) As Double
The second value can be returned through returnValue2. Make sense?
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Aug 16th, 2006, 06:45 PM
#3
Thread Starter
Frenzied Member
Re: [2005] return 2 values from a function?
 Originally Posted by DNA7433
You have to use an output parameter, you cannot return two values the traditional way.
Public Function MyFunction(ByRef returnValue2 As Double) As Double
The second value can be returned through returnValue2. Make sense?
not really...
-
Aug 16th, 2006, 07:13 PM
#4
Re: [2005] return 2 values from a function?
A function returns one object; no more, no less. That's the definition of a function: a method that returns a value. Now having said that a function returns one object, that object can be whatever you like. It can be an instance of a structure or class that have multiple properties that each return individual objects, or it can be an array or collection that contains multiple objects.
Alternatively you can do what DNA suggests and pass one or more parameters by reference, so that their value can be set withing the function and then passed back out again. An example of this is the Date.TryParse method. It needs to pass out two values: a boolean to indicate whether the operation was successful and the Data object that was created if it was successful. To do this its return type is Boolean and it has a Date argument passed by reference. That means that if the operation is successful, that parameter contains the Date after the method returns, e.g.
VB Code:
Dim returnValue As Boolean
Dim byRefParameter As Date
returnValue = Date.TryParse(someString, byRefParameter)
If returnValue = True Then
'The operation was successful so the date value is contained in the second parameter.
MessageBox.Show("The data is " & byRefParameter.ToString())
End If
-
Aug 17th, 2006, 08:43 AM
#5
Re: [2005] return 2 values from a function?
Or you can create a structure containing variables to hold the values that you need then have your function return the structure.
-
Aug 17th, 2006, 05:05 PM
#6
Re: [2005] return 2 values from a function?
 Originally Posted by stanav
Or you can create a structure containing variables to hold the values that you need then have your function return the structure.
I agree.
 Originally Posted by jmcilhinney
that object can be whatever you like. It can be an instance of a structure or class that have multiple properties that each return individual objects
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|