return multiple variables from a fn.? 
Author Message
 return multiple variables from a fn.?

Hi - I've always used subroutines in vb when I wanted to return more than
one value and BYREFed the arguments so I could get the values back - but is
it possible to do with Functions in VB5- does it need variant types or
arrays or userdeifned type - I would like something semantically similar to
like

public function returncoordinates(name) as integer, integer

any ideas?



Sun, 07 Oct 2001 03:00:00 GMT  
 return multiple variables from a fn.?
You can only return 1 variable with a function as far as I'm aware.
There's nothing wrong with the way you're handling it now. :)
Quote:

> Hi - I've always used subroutines in vb when I wanted to return more than
> one value and BYREFed the arguments so I could get the values back - but is
> it possible to do with Functions in VB5- does it need variant types or
> arrays or userdeifned type - I would like something semantically similar to
> like

> public function returncoordinates(name) as integer, integer

> any ideas?



Sun, 07 Oct 2001 03:00:00 GMT  
 return multiple variables from a fn.?

Quote:

>Hi - I've always used subroutines in vb when I wanted to return more than
>one value and BYREFed the arguments so I could get the values back - but is
>it possible to do with Functions in VB5- does it need variant types or
>arrays or userdeifned type

Variants are perhaps the best and most flexible way. I often return complex
arrays stored in a variant from function calls. If the function fails, I set
the variant to Empty, so that I can use the IsEmpty() test to determine it.

You can also use UDTs and, in VB6, arrays as return values from functions.

Frank Carr



Sun, 07 Oct 2001 03:00:00 GMT  
 return multiple variables from a fn.?

Quote:
> > Hi - I've always used subroutines in vb when I wanted to return more
than
> > one value and BYREFed the arguments so I could get the values back - but
is
> > it possible to do with Functions in VB5- does it need variant types or
> > arrays or userdeifned type - I would like something semantically similar
to
> > like

> > public function returncoordinates(name) as integer, integer

> > any ideas?
> You can only return 1 variable with a function as far as I'm aware.
> There's nothing wrong with the way you're handling it now. :)

You could technically return an array or a variant but it seems just as easy
in your case to return the coordinates as byref'd parameters.  If you don't
want to require that users of your function (including you!) declare a
variable for the param if they don't care about it, make it an optional
parameter.


Sun, 07 Oct 2001 03:00:00 GMT  
 return multiple variables from a fn.?
You may only get to return one variable, but that variable can
be pretty versatile.  Consider this function to convert Twips to Pixels:

Public Function TwipsToPixelX(X)
Static Sx
    Sx = Screen.TwipsPerPixelX
    If IsNumeric(X) Then
        TwipsToPixelX = X \ Sx
    ElseIf IsArray(X) Then
        ReDim XX(0 To UBound(X))
        Static i&
        For i = 0 To UBound(X)
            XX(i) = X(i) \ Sx
        Next i
        TwipsToPixelX = XX
    End If
End Function

You would expect these would work:

    Print TwipsToPixelX(45)
    X = TwipsToPixelX(Array(15, 30, 45))
    Print X(0), X(1), X(2)

This also will work (oddly enough):

    Print TwipsToPixelX(Array(150, 300, 450))(2)

The value printed is the converted value of the third parameter
in the list.

LFS

Quote:

> You can only return 1 variable with a function as far as I'm aware.
> There's nothing wrong with the way you're handling it now. :)


> > Hi - I've always used subroutines in vb when I wanted to return more than
> > one value and BYREFed the arguments so I could get the values back - but is
> > it possible to do with Functions in VB5- does it need variant types or
> > arrays or userdeifned type - I would like something semantically similar to
> > like

> > public function returncoordinates(name) as integer, integer

> > any ideas?



Mon, 08 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Excel VBA - How to return multiple parameters/variables via a sub

2. Can a function return multiple variable results?

3. Calling a fn in a parent form from a subform

4. string processing question with Now() fn

5. FutureBASIC: FN PTINRECT

6. Append from fn.ext delimited with ^

7. VB6CE Left Fn

8. Help: Need to call Excel marco fn via DDE

9. Need HWnd given fn

10. popupmenu command yields sub or fn not defined from module

11. Call Fn Pointer?

12. API fn to Change Active Program

 

 
Powered by phpBB® Forum Software