VB6值传递和引用传递 [英] VB6 pass by value and pass by reference

查看:25
本文介绍了VB6值传递和引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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.

推荐答案

这是 VB6 中的经典问题.VB6 手册中有说明.在下面的代码中,VB6 将参数视为表达式(测试)而不是变量引用

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 允许您将表达式传递给 ByRef 参数,即使方法更改了它们.比如你可以写

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

TestFunction (Test + 2)

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

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

如果 TestFunction 采用如下两个参数,您还可以让编译器创建临时副本:

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

TestFunction (one), (two)

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

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

Call TestFunction((Test))

这篇关于VB6值传递和引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆