Results 1 to 7 of 7

Thread: How do I return multiple values?

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    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

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Sure. The MsgBox displays 2 6 12 20

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim intOne As Integer
    4.     Dim intTwo As Integer
    5.     Dim intThree As Integer
    6.     Dim intFour As Integer
    7.    
    8.     intOne = 1
    9.     intTwo = 2
    10.     intThree = 3
    11.     intFour = 4
    12.    
    13.     DoIt intOne, intTwo, intThree, intFour
    14.    
    15.     MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour
    16.    
    17. End Sub
    18.  
    19. Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer)
    20.  
    21.     intA = intA * 2
    22.     intB = intB * 3
    23.     intC = intC * 4
    24.     intD = intD * 5
    25.    
    26. End Function

  3. #3
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    HI martin a question...

    Originally posted by MartinLiss
    Sure. The MsgBox displays 2 6 12 20

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim intOne As Integer
    4.     Dim intTwo As Integer
    5.     Dim intThree As Integer
    6.     Dim intFour As Integer
    7.    
    8.     intOne = 1
    9.     intTwo = 2
    10.     intThree = 3
    11.     intFour = 4
    12.    
    13.     DoIt intOne, intTwo, intThree, intFour
    14.    
    15.     MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour
    16.    
    17. End Sub
    18.  
    19. Public Function DoIt(intA As Integer, intB As Integer, intC As Integer, intD As Integer)
    20.  
    21.     intA = intA * 2
    22.     intB = intB * 3
    23.     intC = intC * 4
    24.     intD = intD * 5
    25.    
    26. End Function
    Does the function return the values because it is a public function?

    with respect

    bindu

  4. #4
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    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

  5. #5
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    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:
    1. something = DoIt(intOne, intTwo, intThree, intFour)????
    2.  
    3. 'run some code to split up the return of doit (i.e something)
    4. 'then make msgbox = to the bits of something>???
    5.  
    6. MsgBox intOne & " " & intTwo & " " & intThree & " " & intFour

    please explain this i am confused here


    ???


  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    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.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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
  •  



Click Here to Expand Forum to Full Width