Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

vba - VB6 pass by value and pass by reference

I am struggling to understand pass by value and pass by reference in VB6. I understand these concepts fully in Object Oriented programming languages like .NET and Java (I realise that Java doesn't have pass by reference). Have a look at the code below:

Private Sub Form_Load()

Dim Test As Integer
Test = 1
TestFunction Test 'line 5
MsgBox (Test)

End Sub

Private Sub TestFunction(ByVal i As Integer)
    i = i + 1
End Sub

When I put brackets around Test on line 5, then the message box prints 1 as I would expect. Now have a look at the code below:

Private Sub Form_Load()

Dim Test As Integer
Test = 1
TestFunction Test 'line 5
MsgBox Test

End Sub

Private Sub TestFunction(ByRef i As Integer)
    i = i + 1
End Sub

The message box prints 2 as I would expect. However, if you add brackets to line 5 then the message box prints 1 as I would not expect. It appears that the calling function can pass by value even if the variable defined in the called function is ByRef. It appears not to be the case vice versa, i.e. if the called function has a signature with a variable defined as ByVal then it will always be ByVal (even if there are no brackets around the variable in the calling function). What is the thinking behind this in VB6? I realize that this is a basic question in VB6 but it has confused me. I have read the MSDN documentation and I realize that this is all true, however it does not explain the reasoning behind it.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a classic gotcha in VB6. It is explained in the VB6 manual. In this code below, VB6 treats the argument as an expression (Test) rather than a variable reference

TestFunction (Test)

In order to pass a reference to the variable either omit the brackets or use the legacy Call statement (which requires brackets)

TestFunction Test
Call TestFunction(Test)

VB6 allows you to pass expressions to ByRef arguments even if the method changes them. Eg you can write

TestFunction (Test + 2)

The compiler creates a temporary copy and passes that by reference. VB.Net uses brackets in a similar way.

You can also get the compiler to create temporary copies if TestFunction takes two arguments like this:

TestFunction (one), (two)

And you can get temporary copies even with Call if you double your brackets, adding an extra unecessary pair:

Call TestFunction((Test))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...