|
-
Nov 1st, 2003, 10:08 PM
#1
Thread Starter
Addicted Member
How do I return multiple values?
I tried making a function that took 4 varibles ByRef in hopes that function could change them and when the function ended the new values would be back in the caller function. Like a reference should, right?
Is there anyway to get this done?
Thanks,
NOMAD
-
Nov 2nd, 2003, 01:25 AM
#2
Sure. The MsgBox displays 2 6 12 20
VB Code:
Private Sub Form_Load()
Dim intOne As Integer
Dim intTwo As Integer
Dim intThree As Integer
Dim intFour As Integer
intOne = 1
intTwo = 2
intThree = 3
intFour = 4
DoIt intOne, intTwo, intThree, intFour
MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour
End Sub
Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer)
intA = intA * 2
intB = intB * 3
intC = intC * 4
intD = intD * 5
End Function
-
Nov 2nd, 2003, 03:21 AM
#3
Lively Member
HI martin a question...
Originally posted by MartinLiss
Sure. The MsgBox displays 2 6 12 20
VB Code:
Private Sub Form_Load()
Dim intOne As Integer
Dim intTwo As Integer
Dim intThree As Integer
Dim intFour As Integer
intOne = 1
intTwo = 2
intThree = 3
intFour = 4
DoIt intOne, intTwo, intThree, intFour
MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour
End Sub
Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer)
intA = intA * 2
intB = intB * 3
intC = intC * 4
intD = intD * 5
End Function
Does the function return the values because it is a public function?
with respect
bindu
-
Nov 2nd, 2003, 03:30 AM
#4
Fanatic Member
function being public does not return any thing... its the scope.
Code:
Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer) as integer
intA = intA * 2
intB = intB * 3
intC = intC * 4
intD = intD * 5
' DoIt= some thing the return value
End Function
-
Nov 2nd, 2003, 05:19 AM
#5
Lively Member
Maritn hasn't got any return datatype after the following
Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer) ?????
intA = intA * 2
intB = intB * 3
intC = intC * 4
intD = intD * 5
End Function
Originally posted by khalik_ash
function being public does not return any thing... its the scope.
Code:
Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer) as integer
intA = intA * 2
intB = intB * 3
intC = intC * 4
intD = intD * 5
' DoIt= some thing the return value
End Function
scope is public yes , but the function has no return???????
Public Function DoIt, how do the values inside the function get bacl to the calling sub????
i thought you had to use
VB Code:
something = DoIt(intOne, intTwo, intThree, intFour)????
'run some code to split up the return of doit (i.e something)
'then make msgbox = to the bits of something>???
MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour
please explain this i am confused here
???

-
Nov 2nd, 2003, 11:32 AM
#6
binduau
The function defaults to returning a Variant if you do not supply a value. What MartinLiss is doing is changing the passed variables in place. Meaning what the function does basically is pass pointers to the variables in the list.
Now when you update these variables you are really updating the actual variables. So there is no need for a return variable.
You could just as easily used a Sub instead of a function.
-
Nov 2nd, 2003, 12:52 PM
#7
I should have made it a Sub since there is no returned value. To make it clearer I should also have done
Public Sub DoIt(ByRef intA As Integer, ByRef intB As Integer, ByRef intC As Integer, ByRef intD As Integer)
Even though ByRef is the default it's not a bad idea to always show it. BTW, ByRef means "by reference" and what that means is that the address of the sent variable is used by the Sub or Function to change the variable, as opposed to ByVal which just uses the value of the variable.
Going back to DoIt for a minute, you might want to consider doing this
Public Function DoIt(ByRef intA As Integer, ByRef intB As Integer, ByRef intC As Integer, ByRef intD As Integer) As Boolean and return True if whatever validation you want to do in the function and everything else goes OK or False if it doesn't.
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
|