数组参数必须为ByRef [英] Array argument must be ByRef

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

问题描述

我在做什么错了?

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

如果在Main中将患者更改为变量数组,并且CalculateScores的参数运行良好,但是我看不到无法传递字符串的逻辑.默认情况下,它是ByRef,所以我知道我丢失了一些东西.

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.

推荐答案

执行此操作时:

DoSomething (expression)

您要强制将expression评估为,并传递ByVal,无论该参数是否明确表示已传递ByRef .尽管在大多数情况下这几乎没有影响,但是当您尝试传递数组或对象引用时,它会在后端咬住您.

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.

删除括号.

DoSomething expression


现在,您的代码还有其他问题:您正在将字符串数组传递给String参数;那行不通.将参数设置为数组或Variant,为清楚起见,建议您将参数明确标记为ByRef.


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.

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

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