Dose byref really work in vba

T

topalps

I'm confused with 'byref' now. the i should be changed, doesn't it? but
it turns out that i never changed. why? dose referenece really work in
vba?

Sub try()
Dim i As Integer
i = 1
Debug.Print i
f1 (i)
Debug.Print i
Dim j As Integer
f2(i)
Debug.Print i
End Sub

Private Function f1(ByVal n As Integer)
n = n + 1
End Function

Private Function f2(ByRef n As Integer) As Integer
n = n + 1
End Function
 
C

Cindy M.

I'm confused with 'byref' now. the i should be changed, doesn't it? but
it turns out that i never changed. why? dose referenece really work in
vba?
It works. It's just that you're overriding it by putting the value you're
passing to the function in parentheses. Take away the parentheses (or
assign the result of the function to another variable) and ByRef is used.
Sub try()
Dim i As Integer
i = 1
Debug.Print i
f1 (i)
Debug.Print i
Dim j As Integer
f2(i)
Debug.Print i
End Sub

Private Function f1(ByVal n As Integer)
n = n + 1
End Function

Private Function f2(ByRef n As Integer) As Integer
n = n + 1
End Function

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

topalps

It works. It's just that you're overriding it by putting the value you're
passing to the function in parentheses. Take away the parentheses (or
assign the result of the function to another variable) and ByRef is used.

yeah, it works!
and it does by using 'call' explicitly, keeping the parentheses.
never know it can be override like this, I don't like this.
 
C

Cindy M.

It works. It's just that you're overriding it by putting the value you're
yeah, it works!
and it does by using 'call' explicitly, keeping the parentheses.
never know it can be override like this, I don't like this.
Well, VBA is extremely "flexible" about allowing all kinds of things. If
you're coming from a "strict" language, you won't like lots of things...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
Top