ByVal和ByRef VBA [英] ByVal vs ByRef VBA

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

问题描述

我已经尝试过JaredPar ByRef vs ByVal Clarification

VB.NET中的

ByVal表示将发送提供的值的副本 功能.对于值类型(IntegerSingle等),这将 提供值的浅表副本.对于较大的类型,可以 效率低下.但是对于引用类型(String,类实例) 引用的副本已传递.因为副本是通过突变传递的 通过=设置为参数,调用函数将看不到它.

VB.NET中的

ByRef表示对原始值的引用将是 发送到函数(1).就像原始值一样 直接在函数内使用. =之类的操作会影响 原始值,并在调用函数中立即可见.

我尝试使用以下代码对其进行测试,但似乎无法使用ByRef将该单元格的值更改为0(如果它是1

这是我正在测试的以下代码,我做错了什么? Range("A1")的值仍为1

Sub test1()

    Dim trythis As Boolean

    trythis = False

    If (Sheets("TESTING").Range("A1").value = 1) Then
        testingRoutine (trythis)
        If (trythis) Then
            Debug.Print "Value changed to 0"
            Sheets("TESTING").Range("A1").value = 0
        End If
    End If

End Sub

Private Function testingRoutine(ByRef trythis As Boolean)

    Debug.Print "Ran Function"
    trythis = True

End Function

解决方案

VB子例程不需要在参数列表中使用大括号.但是,如果传递单个参数并将其括在括号中,则传递的是 expression .表达式不能通过引用传递.因此,您必须在调用testingRoutine (trythis)中删除括号并写出testingRoutine trythis

注意:如果在不使用函数返回值的情况下调用函数,则必须将其编写为过程调用(在参数列表的中间没有大括号).例如:

myVal = myFunction (trythis)   ' trythis will be passed by reference
myFunction (trythis)           ' trythis will be seen as an expression
myFunction trythis             ' trythis will be passed by reference

myVal = mySub (trythis)        ' invalid: mySub is not a function
mySub (trythis)                ' trythis will be seen as an expression
mySub trythis                  ' trythis will be passed by reference

当然,当一个函数或子函数具有多个参数时,该问题将立即解决,因为逗号不能出现在表达式中.

I've tried to attempt something that was answered by JaredPar ByRef vs ByVal Clarification

ByVal in VB.NET means that a copy of the provided value will be sent to the function. For value types (Integer, Single, etc.) this will provide a shallow copy of the value. With larger types this can be inefficient. For reference types though (String, class instances) a copy of the reference is passed. Because a copy is passed in mutations to the parameter via = it won't be visible to the calling function.

ByRef in VB.NET means that a reference to the original value will be sent to the function (1). It's almost like the original value is being directly used within the function. Operations like = will affect the original value and be immediately visible in the calling function.

And I've tried to test it with the following code and I can't seem to get it to work use the ByRef to change the value of the cell to 0 if it's 1

Here's my below code that I'm testing with, what am I doing wrong? The value of Range("A1") is still 1

Sub test1()

    Dim trythis As Boolean

    trythis = False

    If (Sheets("TESTING").Range("A1").value = 1) Then
        testingRoutine (trythis)
        If (trythis) Then
            Debug.Print "Value changed to 0"
            Sheets("TESTING").Range("A1").value = 0
        End If
    End If

End Sub

Private Function testingRoutine(ByRef trythis As Boolean)

    Debug.Print "Ran Function"
    trythis = True

End Function

解决方案

VB subroutines don't require braces around the argument list. However, if you pass a single argument and you enclose that in braces, you are passing an expression . Expressions cannot be passed by reference. Therefore you must remove the braces in the call testingRoutine (trythis) and write testingRoutine trythis

Note: if you call a function without using its return value, it must be written as a procedure call (without braces around the argument list). As an example:

myVal = myFunction (trythis)   ' trythis will be passed by reference
myFunction (trythis)           ' trythis will be seen as an expression
myFunction trythis             ' trythis will be passed by reference

myVal = mySub (trythis)        ' invalid: mySub is not a function
mySub (trythis)                ' trythis will be seen as an expression
mySub trythis                  ' trythis will be passed by reference

Of course, the problem will be clear immediately when a function or sub has more than one parameter because a comma cannot appear in an expression.

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

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