vba VB6传值和传引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10262186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:53:23  来源:igfitidea点击:

VB6 pass by value and pass by reference

vbavbscriptvb6

提问by w0051977

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:

我正在努力理解 VB6 中的值传递和引用传递。我在 .NET 和 Java 等面向对象的编程语言中完全理解这些概念(我意识到 Java 没有通过引用传递)。看看下面的代码:

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:

当我在第 5 行的 Test 周围加上括号时,消息框会按照我的预期打印 1。现在看看下面的代码:

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.

消息框按我的预期打印 2。但是,如果您将括号添加到第 5 行,那么消息框会像我所期望的那样打印 1。即使被调用函数中定义的变量是ByRef,调用函数似乎也可以按值传递。反之,情况似乎并非如此,即如果被调用函数具有一个变量定义为 ByVal 的签名,那么它将始终是 ByVal(即使调用函数中的变量周围没有括号)。VB6 背后的想法是什么?我意识到这是 VB6 中的一个基本问题,但它让我感到困惑。我已经阅读了 MSDN 文档,我意识到这一切都是真的,但是它并没有解释其背后的原因。

回答by MarkJ

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

这是 VB6 中的一个经典问题。它在 VB6 手册中解释。在下面的这段代码中,VB6 将参数视为表达式(Test) 而不是变量引用

TestFunction (Test)

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

为了传递对变量的引用,要么省略括号,要么使用遗留Call语句(需要括号)

TestFunction Test
Call TestFunction(Test)

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

VB6 允许您将表达式传递给ByRef参数,即使方法更改了它们。例如你可以写

TestFunction (Test + 2)

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

编译器创建一个临时副本并通过引用传递它。VB.Net以类似的方式使用括号

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

如果 TestFunction 接受两个这样的参数,您还可以让编译器创建临时副本:

TestFunction (one), (two)

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

即使Call您将括号加倍,添加额外的不必要的对,您也可以获得临时副本:

Call TestFunction((Test))

回答by Jonathan Wood

Enclosing any expression within parentheses causes that expression to be evaluated first before doing anything else, even when that expression is only a single variable. In your case, the result of that expression is then passed as an argument.

将任何表达式括在括号内会导致该表达式在执行任何其他操作之前首先被评估,即使该表达式只是一个变量。在您的情况下,该表达式的结果然后作为参数传递。

So, you are in fact passing the argument by reference. But the argument you are passing is the result of the expression and not the original variable. This is why the original variable does not get updated.

因此,您实际上是通过引用传递参数。但是您传递的参数是表达式的结果,而不是原始变量。这就是原始变量没有更新的原因。