首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ByVal对ByRef VBA

ByVal对ByRef VBA
EN

Stack Overflow用户
提问于 2017-10-26 17:00:51
回答 4查看 17.4K关注 0票数 6

我试着尝试一些由JaredPar ByRef vs ByVal Clarification回答的事情

ByVal在VB.NET中意味着将向函数发送所提供的值的副本。用于值类型(IntegerSingle等)这将提供该值的浅表副本。对于较大的类型,这可能是低效的。但是,对于引用类型(String,类实例),将传递引用的副本。由于副本是通过=以突变形式传递给参数的,所以调用函数不会看到它。 ByRef在VB.NET中意味着对原始值的引用将发送给函数(1)。这几乎就像在函数中直接使用原始值一样。像=这样的操作将影响原始值,并在调用函数中立即可见。

我试着用下面的代码测试它,如果它是ByRef,我似乎无法让它工作,使用0将单元格的值更改为1

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

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-10-26 17:12:00

VB子程序不需要参数列表周围的大括号。但是,如果传递单个参数并将其括在大括号中,则传递一个表达式。表达式不能通过引用传递。对它们进行评估,并通过它们的结果。因此,必须删除调用testingRoutine (trythis)中的大括号并编写testingRoutine trythis

注意:如果不使用函数的返回值而调用函数,则必须将其编写为过程调用(参数列表不带大括号)。例如:

代码语言:javascript
复制
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

当然,当一个函数或子函数有多个参数时,问题就会很清楚,因为逗号不能出现在表达式中。

票数 12
EN

Stack Overflow用户

发布于 2017-10-26 17:08:32

改变这一点:

testingRoutine (trythis)

对此:

testingRoutine trythis

那就试试吧。

另外,看看如果我将它改为这个函数会发生什么,其中函数被用作函数(返回某些内容)。

代码语言:javascript
复制
Sub test1()

    Dim trythis As Boolean
    Dim testit As Boolean

    trythis = False

    If (Sheets("Sheet1").Range("A1").Value = 1) Then
        testit = 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) As Boolean

    Debug.Print "Ran Function"
    trythis = True

End Function
票数 3
EN

Stack Overflow用户

发布于 2017-10-26 17:13:56

我会这样做的。

代码语言:javascript
复制
Sub test1()

    Dim trythis As Boolean

    trythis = False

    If (Sheets("TESTING").Range("A1").value = 1) Then
        tr = testingRoutine(trythis)
        If tr 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"
    testingRoutine = True

End Function

因为trythis不是一个全局变量,所以在函数中更改它不会在子变量中起任何作用。它将只在函数中将trythis定义为True,并保持在该范围内。要使trythis受到函数的影响并由子函数读取,您必须将其赋值给子函数中的一个变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46959921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档