VBAhole22
06-16-2003, 12:30 PM
I have a handy routine that projects x and y coordinates to different projection systems. It takes 2 numbers and 2 string values and returns 2 numbers. I copy this routine into several different places in my code for different forms. Of course, I need to tweak the code every now and then and I find myself doing it 3 or four times for each place the code is. So I would like to make it a function but don't functions only return one value? How can I get my function to return several values?
dcx693
06-16-2003, 12:54 PM
There is no way around it: functions only return one value. However, you can concatentate values into strings, separated by a delimiter of your choosing if you must pass multiple return values like "0;1;1;2;3;5;8" etc...
VBAhole22
06-16-2003, 01:00 PM
That is a bummer. Is there anyway around this by setting up user classes or data types?
dcx693
06-16-2003, 01:32 PM
Hmmm...wait, my answer might not have been totally accurate. Can someone else chime in before I start misleading people?
VBAhole22
06-16-2003, 01:59 PM
Right on!! I have read something to that extent about 10 times but it never sunk in till now. Sometimes you have to keep knocking on the door to get in!
Thanks
ChrisO
06-16-2003, 04:49 PM
Here's another possibility: -
Option Explicit
Option Compare Text
Public Type udtUserDataType
lngX As Long
lngY As Long
strOne As String
strTwo As String
End Type
Sub Test()
Dim lngXPosition As Long
Dim lngYPosition As Long
Dim strFirstString As String
Dim strSecondString As String
Dim udtNewUserDataType As udtUserDataType
lngXPosition = 1234
lngYPosition = 5678
strFirstString = "ABC"
strSecondString = "DEF"
udtNewUserDataType = MyTestFunction(lngXPosition, lngYPosition, strFirstString, strSecondString)
MsgBox lngXPosition & ", " & udtNewUserDataType.lngX & vbTab & _
" -:- " & vbTab & _
lngYPosition & ", " & udtNewUserDataType.lngY & vbNewLine & _
strFirstString & ", " & udtNewUserDataType.strOne & vbTab & _
" -:- " & vbTab & _
strSecondString & ", " & udtNewUserDataType.strTwo
End Sub
Public Function MyTestFunction(ByVal lngMyX As Long, _
ByVal lngMyY As Long, _
ByVal strFirst As String, _
ByVal strSecond As String) As udtUserDataType
lngMyX = lngMyX * 2
lngMyY = lngMyY * 2
strFirst = strFirst & CStr(lngMyX)
strSecond = strSecond & CStr(lngMyY)
MyTestFunction.lngX = lngMyX
MyTestFunction.lngY = lngMyY
MyTestFunction.strOne = strFirst
MyTestFunction.strTwo = strSecond
End Function
Hope that helps.
Regards
Chris
VBAhole22
06-17-2003, 05:57 AM
Now that is probably more close to the way it 'should' be done. But I like both methods because the first requires little overhead for a small function while the user data type seems more 'full featured'.
Thanks for the great suggestions from all.
WayneRyan
06-17-2003, 12:52 PM
VBAhole22,
You could also have the function return a Variant, which can
be an array.
Wayne
dcx693
06-17-2003, 01:59 PM
You could also have the function return a Variant, which can That's actually what I was thinking of. However, I've never written such a function, so I couldn't comment.