Solved

Returning several values from a procedure or function

Posted on 2002-06-19
7
162 Views
Last Modified: 2010-05-02
hi,

I would like to get several return values from a sub or function. how can i do that because from my knowledge, i just understand that a function can return 1 value whilst we r not returning value(s) from a sub.

thanks.
0
Comment
Question by:lslim
7 Comments
 
LVL 3

Accepted Solution

by:
nigelrowe earned 50 total points
ID: 7092176
How about...

Private Sub Command1_Click()
Dim an_array As Variant
    an_array = ReturnValues
End Sub


Private Function ReturnValues() As Variant
Dim i As Integer, s As String, o As Object
   i = 1
   s = "A string"
   Set o = New Collection
   ReturnValues = Array(i, s, o)
End Function
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092226
Another way (if you don't need the flexibility of arrays of return values):

Private Function ReturnValues(byref i As Integer, byref s As String, byref o As Object ) As Long
on error goto Err
  i = 1
  s = "A string"
  Set o = New Collection

  'return 0 ->success
Exit function
Err:
  ReturnValues = err.number
   
End Function

CHeers
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 7092354
yet another one


Private Function ReturnValues() As Long()

and so on .....
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Expert Comment

by:bob_online
ID: 7092427
If you declare the arguements of a procedure ByRef, you can modify them in the sub (or function)

sub main()

   dim St as string
   dim Nu as integer

   ST = "Going In"
   NU = 9
   Msgbox ST & " " & Nu

   Modify_Us St, Nu

   Msgbox ST & " " & Nu

End sub

Private Sub Modify_Us(ByRef St1 as string,ByRef nu1 as Integer)

   St1 = "Coming Out"
   Nu1 = 1

End Sub

You can modify any number of parameters this way without resorting to arrays or collecitons.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7093346
If you want to maintain the concept of functions returning only a single thing, you can have it return a structure (i.e. object, user-defined type, array, or elementary variable)

The elementary will return a single value, which will only help if you make it a string and parse it on the other end.  This is not the best approach.

The array will return multiple items of the same type, and was listed as a choice in a previous comment.

The user-defined type lets you defined a collection of values to the referred to by a single name.

The object is essentially a user-defined type with more.

--
As indicated above, you can also break the one-value return by setting an "input" parameter as ByRef, after which any change to that valiable will impact the calling procedure also.

Also Microsoft uses this concept for some of its API calls, it is not recommended because it does not clearly indicate what will change.  Clarification: return values are expected to change; object properties are expected to change; input parameters should NOT change.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7922706
Hi lslim,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: nigelrowe and angelIII

lslim, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7968504
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

angelIII points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20517678.html
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now