Solved

Returning several values from a procedure or function

Posted on 2002-06-19
7
168 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 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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