Array argument must be ByRef

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

What am I doing wrong here?

Sub Main()  Dim patients() As String  ' Some code to populate the patients array, works fine  CalculateScores (patients) ' Array argument must be ByRef compile error  End Sub    Sub CalculateScores(patients As String)  End Sub 

If I change patients to a variant array in Main and the parameters of CalculateScores it works fine but I can't see the logic of not being able to pass a string. By default it is ByRef so I know I'm missing something.

I can use a variant sure, but it feels hacky.

回答1:

When you do this:

DoSomething (expression) 

You're forcing expression to be evaluated as a value, and passed ByVal, regardless of whether the parameter explicitly says it's passed ByRef. While that has more or less no impact most of the time, it bites you in the rear end when you try to pass an array or an object reference.

Drop the parentheses.

DoSomething expression 

Now, there are other problems with your code: You're passing an array of strings into a String parameter; that can't work. Make the parameter an array or a Variant, and I'd suggest marking the parameter explicitly as ByRef, for clarity.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!