Need to return multiple values via same function using VB6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashokbio
    New Member
    • Jul 2007
    • 14

    #1

    Need to return multiple values via same function using VB6

    I want to return values of two arguments through a function via same argument.

    Example:

    (a, b) = getvalue(x, y)

    Can anyone help?



    T. Ashok Kumar
    Last edited by Killer42; Jul 30 '07, 02:29 AM. Reason: Removed e-mail address as per site guidelines
  • kentgorrell
    New Member
    • Jul 2007
    • 11

    #2
    A function can only return a single value but if you were to concatenate the two values that you want to return eg-
    value1 & ", " & value2
    so if you pass in (x, y) and you get back "24, 25" - note the result will be a string.

    Comment

    • joshtboy9987
      New Member
      • Jul 2007
      • 5

      #3
      You could pass the variables you want the function to write to by reference. (Change the function to a sub).

      for example

      Sub Add(a as single, b as single, ByRef c as single, ByRef d as single)

      c=a+b
      d=b+c

      End Sub

      The following code would change the values of c and d to a+b and b+c respectively.
      This kind of acts like a pointer in C. Instead of sending a value to the function
      it sends a address so that the program can modify the value directly...

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by kentgorrell
        A function can only return a single value but if you were to concatenate the two values that you want to return eg- ...
        Are you sure about that? I have a feeling it might be possible for a function to return an array. Or a variant, which can contain an array.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by joshtboy9987
          You could pass the variables you want the function to write to by reference. (Change the function to a sub).
          for example
          Sub Add(a as single, b as single, ByRef c as single, ByRef d as single) ...
          Just wanted to point out, arguments are passed by reference by default in VB. You only need to specify if you want to pass them by value. In other words, while you might sometimes code the ByVal keyword, there is really no need for ByRef.

          As an example, these two definitions produce the same result...
          [CODE=vb]Sub Add(a As Single, b As Single, c As Single, d As Single)
          Sub Add(a As Single, b As Single, ByRef c As Single, ByRef d As Single)[/CODE]

          Comment

          Working...