Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Returning multiple values from function


Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2010, 10:06 AM
barrys barrys is offline
Freshman
 
Join Date: Jan 2010
Posts: 26
Default Returning multiple values from function


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 = ""
Else
AreWeThereYet = False
WhyNot = "Because Daddy doesn't drive fast enough"
End If
End Function
Reply With Quote
  #2  
Old 01-15-2010, 10:50 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,686
Default

The return keyword is for vb.net, not legacy VB.

The legacy equ is:

Code:
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.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 01-15-2010, 10:55 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,686
Default

For example:

Code:
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 Else ' code End If End Function Private Sub Form_Load() Dim t As MyType t = AreWeThereYet(0) End Sub
__________________
Quis custodiet ipsos custodues.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->