Returning multiple values from function

01-15-2010, 10:06 AM
I know this is a dumb question, but I'm new to VB. I have a function that I want to return more than one value for. I've googled this, but can't find a good/working example. I'm using VB6 if that matters. Here is an example (don't mind my crazy sense of humor). I've read that I'm suppose to do (in this example) a RETURN WhyNot, but VB is barfing on that statement

Public Function AreWeThereYet (MilesToGo As Integer, ByVal WhyNot As String) As Boolean

If MilesToGo=0 Then
AreWeThereYet = True
WhyNot = ""
AreWeThereYet = False
WhyNot = "Because Daddy doesn't drive fast enough"
End If
End Function

01-15-2010, 10:50 AM
The return keyword is for, not legacy VB.

The legacy equ is:

FunctionName = value
Exit Function

Exit function is unnecessary if the Function Name assignment is the last line of code.

Note that you want WhyNot to be ByRef and not ByVal.

Alternatively, return a UDT with a string and a boolean.

01-15-2010, 10:55 AM
For example:

Option Explicit

Private Type MyType
reason As String
thereYet As Boolean
End Type

Friend Function AreWeThereYet(ByVal lMilesToGo As Long) As MyType
If lMilesToGo = 0 Then
AreWeThereYet.reason = "blah"
AreWeThereYet.thereYet = True
' code
End If
End Function

Private Sub Form_Load()
Dim t As MyType

t = AreWeThereYet(0)
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum