Link to home
Start Free TrialLog in
Avatar of lslim
lslim

asked on

Returning several values from a procedure or function

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.
ASKER CERTIFIED SOLUTION
Avatar of nigelrowe
nigelrowe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guy Hengel [angelIII / a3]
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
Avatar of corvanderlinden
corvanderlinden

yet another one


Private Function ReturnValues() As Long()

and so on .....
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.
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.
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
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

angelIII points for you at:
https://www.experts-exchange.com/questions/20517678/points-for-angelIII-re-20313630.html