Return values from subroutine call

S

Steve S

A function will only return 1 value but I need to return 2 values so can I
use a subroutine? The following statement would be the call and A and B are
the values I want to pass. C and D are the 2 values I want returned. Is
this possible??

CAll subX(1,2, me.TextBoxC, me.TextBoxD)

SubX(A,B,byRef, D ByRef)
C=A+B
D=A-B
end sub

I would expect TextBoxC to contain 3 and TextBoxD to contain -1 but this
doesn't seem to work. What am I missing. Other programming languages
support this concept.
 
S

strive4peace

Hi Steve,

by default, parameters are passed ByRef (by reference), which means they
can be changed if the parameter was a variable

you can override the default and pass ByVal (by value), which means that
the changed value is not passed back

example:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub TestReturnParameter()

Dim mByRefParameter As String _
, mByValParameter As String

mByRefParameter = "before"
mByValParameter = "before"

ChangeParameter mByRefParameter, mByValParameter

MsgBox "ByRefParameter: " & mByRefParameter _
& vbCrLf & "ByValParameter: " & mByValParameter _
, , "After running ChangeParameter"

End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub ChangeParameter( _
ByRef pByRefParameter As String _
, ByVal ByValParameter As String)

pByRefParameter = "after"
ByValParameter = "after"
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ByRef is the default for passed parameters, so you do not need to
specify it; I did to make it more obvious what is happening


Warm Regards,
Crystal

remote programming and training

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
:) have an awesome day :)
*
 
A

Albert D. Kallal

Steve S said:
A function will only return 1 value but I need to return 2 values so can I
use a subroutine? The following statement would be the call and A and B
are
the values I want to pass. C and D are the 2 values I want returned. Is
this possible??

CAll subX(1,2, me.TextBoxC, me.TextBoxD)

SubX(A,B,byRef, D ByRef)
C=A+B
D=A-B
end sub

Legal syntax is:

Public Sub subx(A, B, C, D)

C = A + B
D = A - B

End Sub

However, the above defaults all parameters to type "variant" which accepts
any type of value/object. Likely better to define the types and then the
compiler will catch errors at compile time. (you should frequlty go
debug->compile after you modify code to force a compile)

So, the above really should be have been defined as:

Sub subx(A As Integer, B As Integer, C As Control, D As Control)

C = A + B
D = A - B

End Sub


If you have a lot of parameters, you could split each one it into one line
per
parameters such as:

Sub subx(A As Integer, _
B As Integer, _
C As Control, _
D As Control)

C = A + B
D = A - B

End Sub

Not a big deal for the above, but for stuff like:

Public Function BookAndCreate(RidesTour As CRide, _
SelectedOptions As Collection, _
BusId As Long, _
tblBookingid As Long, _
GSize As Integer, _
Optional OTourGroupId As Long) As Long

So, if the line is long, you can split it up like above...

Note in our example we are using "control" as a type and the "default"
property of a control on a form is "value", so one really could write the
our code above as:

Sub subx(A As Integer, _
B As Integer, _
C As Control, _
D As Control)

C.Value = A + B
D.Value = A - B

End Sub

You notice that when you type the above, because we correctly "typed" the
control type, inteli-sense does work as you type the "c" and "d" values in
the code editor to complete the .value property of the control

Also, by-ref is the default passing method. Since a and b are NOT to be
modified, then better yet is:

Sub subx(ByVal A As Integer, _
ByVal B As Integer, _
C As Control, _
D As Control)

C.Value = A + B
D.Value = A - B

End Sub


And, adding by-ref for the values for sake of clarity, we we get:

Sub subx(ByVal A As Integer, _
ByVal B As Integer, _
ByRef C As Control, _
ByRef D As Control)

C.Value = A + B
D.Value = A - B

End Sub

Since the ByRef is the default s a general rule I don't type it in.
However, for clarify, I do useally type in the byVal.

The same goes for the .value of a contorl. I leave
out .value, but there are cases when this will bite in you in code.

Such as:

colMylist.Add c

or

colMyList.Add c.Value

In the first example .add c, we actually adding the **control** to our
custom collection, and in the 2nd example we are adding the value of the
control to that custom collection. So sometimes leaving out the .value can
really change the meaning of your code...
 
S

Steve S

Thanks Albert.

The 'X as control' is what I was missing. Placing this code in a public sub
called from about 9 different forms allowed me to delete about 60 lines of
code.
 
A

Albert D. Kallal

Steve S said:
Thanks Albert.

The 'X as control' is what I was missing. Placing this code in a public
sub
called from about 9 different forms allowed me to delete about 60 lines of
code.

--

Great, I mean the most simply syntax of:

Public Sub subx(A, B, C, D)

C = A + B
D = A - B

End Sub

Would work fine, but as you see, I rather expanded on this .....
 
D

David W. Fenton

The same goes for the .value of a contorl. I leave
out .value, but there are cases when this will bite in you in
code.

Such as:

colMylist.Add c

or

colMyList.Add c.Value

In the first example .add c, we actually adding the **control** to
our custom collection, and in the 2nd example we are adding the
value of the control to that custom collection. So sometimes
leaving out the .value can really change the meaning of your
code...

Huh! I didn't know that. That's something of an inconsistency. On
the other hand, without it, there'd be no way to add the control
reference to the collection.

I need to have a think on that one -- thanks for pointing it out,
Albert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top