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...