Type mismatch when passing string variable into Variant parameter, and assigning result of Array function to parameter

后端 未结 2 1470
囚心锁ツ
囚心锁ツ 2020-12-11 20:27

I have the following two procedures:

Sub OuterSub()
    Dim s As String
    s = \"Lorem ipsum dolor sit amet\"
    InnerSub s
End Sub

Sub InnerSub(prm As Va         


        
相关标签:
2条回答
  • 2020-12-11 21:01

    The parameter definition here:

    Sub InnerSub(prm As Variant)
    

    is implicitly ByRef:

    Sub InnerSub(ByRef prm As Variant)
    

    This means that any assignments to prm will also be assigned to s in OuterSub. While an array can be assigned to the Variant variable prm, it cannot be assigned to the String variable s in OuterSub.

    You can see this in action by defining s As Variant in OuterSub (which has no problem holding an array), and inspecting the value of s after InnerSub has completed.

    You can explicitly force a variable to be passed in ByVal:

    Sub InnerSub(ByVal prm As Variant)
    

    Passing a constant:

    Const s = "Lorem ipsum dolor sit amet"
    

    or a string literal:

    InnerSub "Lorem ipsum dolor sit amet"
    

    both work, because neither can be passed in ByRef.

    Wrapping an argument in brackets also forces the variable to be passed in ByVal, which is why the following works

    Sub OuterSub()
        Dim s As String
        s = "Lorem ipsum dolor sit amet"
        InnerSub (s) ' The brackets here do the trick
    End Sub
    

    OTOH, neither of your non-working alternatives work, because whether you create a local string variable, or you wrap the argument to Array in parentheses, the problem is the same -- you're trying to assign an array to a string variable via the ByRef prm parameter.


    See my answer to CallByName won't accept variant arguments. Quoting from the link referenced in that answer:

    How to: Force an Argument to Be Passed by Value (Visual Basic)

    The procedure declaration determines the passing mechanism. If a parameter is declared ByRef, Visual Basic expects to pass the corresponding argument by reference. This allows the procedure to change the value of the programming element underlying the argument in the calling code. If you wish to protect the underlying element against such change, you can override the ByRef passing mechanism in the procedure call by enclosing the argument name in parentheses. These parentheses are in addition to the parentheses enclosing the argument list in the call.

    The calling code cannot override a ByVal mechanism.

    To force an argument to be passed by value If the corresponding parameter is declared ByVal in the procedure, you do not need to take any additional steps. Visual Basic already expects to pass the argument by value.

    If the corresponding parameter is declared ByRef in the procedure, enclose the argument in parentheses in the procedure call.

    0 讨论(0)
  • 2020-12-11 21:02

    I don't know the reason, but it appears that when passing byref the expection is throw. String is passed by reference, but if you put a parenthesis around it, it is passed by value:

    Sub OuterSub()
        Dim s As String
        s = "Lorem ipsum dolor sit amet"
        InnerSub (s)
    End Sub
    

    Another option, if you don't want to change the outer function, is to define the InnerSub prm by value:

    Sub InnerSub(ByVal prm As Variant)
        prm = Array(prm)
    End Sub
    
    0 讨论(0)
提交回复
热议问题