ByVal vs ByRef VBA

后端 未结 4 2027
离开以前
离开以前 2021-02-09 21:25

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

4条回答
  •  耶瑟儿~
    2021-02-09 22:09

    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. They are evaluated and their result is passed. 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.

提交回复
热议问题