?
Solved

Returning several values from a procedure or function

Posted on 2002-06-19
7
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Accepted Solution

by:
nigelrowe earned 200 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 143

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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