Passing variables to Sub problem

G

Gibson

Here is what I have:

Private Sub First()
Dim strWriteLine as String
Second(strWriteLine)
End Sub

Private Sub Second(strWriteLine As String)
strWriteLine = strWriteLine & "Other Stuff"
End Sub

The sub First creates the variable strWriteLine and adds data to it. I then
call the sub Second passing the variable strWriteLine. This works fine. When
I stop the code and check the variable it is passed intact. The sub second
adds "Other Stuff" to the variable correctly. The problem is when sub Second
ends an I am returned to Sub First, strWriteLine is back to its original data
as if it was never passed to sub Second. The "Other Stuff" is no longer there.
How can I cause the strWriteLine to retain the "Other Stuff" added to it in
sub Second after returning to sub First?

Thanks.
 
D

Douglas J. Steele

Get rid of the parentheses (or else use the Call keyword):

Second strWriteLine

or

Call Second(strWriteLine)

The default for passing string parameters is ByRef, which means you're
passing a reference to the variable so that it does get updated within the
called routine. However, when you put parentheses around a variable like
that, it changes it to ByVal, meaning the contents of the variable is
passed, not the reference to it.
 
K

Klatuu

You are not returning anything from your call to Second. Also, in First, I
don't see any assignment of value to strWriteLine. What are you trying to do?
 
Top