returning multiple values from one function

J

Jonathan West

Jezebel said:
An advantage of user-defined types over arrays is that their elements can
be of different data types.

True, though that can also be achieved using an array of Variants
They are also essential for API calls to functions that, in C, take or
return a Struct argument (which is why they were introduced in the first
place) ---

Also true, though not quite relevant to the question, which was about
returning multiple values from routines you write for yourself rather than
being able to call predefined API functions.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

muyBN

Some eight months later after posting my initial question...

"I know that functions are generally made to return one value but I know
that there are also ways to return multiple values with arrays or objects and
dot notation."

....today I finally figured out a full solution based on the replies made
here. I have been limping along all this time by running the macro so that it
goes through the function twice to return the two values that I needed. I was
getting errors when I tried Jezebel's #2 suggestion (using an array) then
Greg's version of what Jezebel had started; so for all this time, what I've
been running was sufficient but not very efficient, so in case anyone else
wants to know, I'll write my solution below.

Greg wrote this solution:

Sub CallTest()
Dim a As Variant
Dim i As Long
a = MyFunct 'calls function below
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Function MyFunct() As String()
Dim localArray(1 To 3) As String
localArray(1) = 1
localArray(2) = 2
localArray(3) = 3
MyFunct = localArray
End Function

As I said, I was getting errors with the array but got a good solution when
I made these changes:

Function MyFunct() As <font color="red">Variant</font>()
Dim localArray(1 To 3) As String
localArray(1) = 1
localArray(2) = 2
localArray(3) = 3
MyFunct = localArray
End Function
 
P

Perry

Here's another one, using a user defined type as function output.

Type MultipleValues
NormalFormat As Date
ShortFormat As Date
C_Longed As Long
End Type

Function GetMultipleValues(ByVal MyDate As Date) As MultipleValues
GetMultipleValues.NormalFormat = MyDate
GetMultipleValues.ShortFormat = Format(MyDate, "d m yyyy")
GetMultipleValues.C_Longed = CLng(MyDate)
End Function

? getmultiplevalues(now).NormalFormat
26-1-2007 11:34:15
? getmultiplevalues(now).ShortFormat
26-1-2007
? getmultiplevalues(now).C_Longed
39108

Krgrds,
Perry
 
M

muyBN

I originally made this post over a year ago but thought I would revisit it
with some new information I found out that might be helpful to other people.

I don't know if this happens in 100% of the cases, but I found that in a
function I was running, when I passed a parameter that had a null value
before returning the main value of the function, that the value generated for
the parameter in the function was also generated. For example:

intInteger 'with null value
strString 'with null value

strValue = GetFunction(intInteger,strString) 'parameters intInteger and
strString are used in function

'after function returns a value for strValue, the previously null-valued
intInteger and strString now have a value as well, as generated during
GetFunction
 
J

Jean-Guy Marcil

muyBN was telling us:
muyBN nous racontait que :
I originally made this post over a year ago but thought I would
revisit it with some new information I found out that might be
helpful to other people.

I don't know if this happens in 100% of the cases, but I found that
in a function I was running, when I passed a parameter that had a
null value before returning the main value of the function, that the
value generated for the parameter in the function was also generated.
For example:

intInteger 'with null value
strString 'with null value

strValue = GetFunction(intInteger,strString) 'parameters intInteger
and strString are used in function

'after function returns a value for strValue, the previously
null-valued intInteger and strString now have a value as well, as
generated during GetFunction

This is because ByRef is the default value for passing arguments. In other
words, the argument may change the original variable value if it gets
modified in the function that uses it.
If you do not want that behaviour, you have to specify ByVal, as in:

Function GetFunction(ByVal intInteger As Long, ByVal strString As String)

Notice that I have declared the variable type, which I believe is a good
habit.
Also, I see you intend to use an Integer, but if you do, the compiler will
convert it to a Long because the smallest memory space a 32 bit computer can
allocate is equivalent to a long. So I do not use the Integer type anymore
as it does not save memory space and actually makes the execution marginally
slower because a conversion has to occur.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top