VBA - 从属性获取返回数组
如果数组是通过引用返回的,为什么下面的方法不起作用:
'Class1 class module
Private v() As Double
Public Property Get Vec() As Double()
Vec = v()
End Property
Private Sub Class_Initialize()
ReDim v(0 To 3)
End Sub
' end class module
Sub Test1()
Dim c As Class1
Set c = New Class1
Debug.Print c.Vec()(1) ' prints 0 as expected
c.Vec()(1) = 5.6
Debug.Print c.Vec()(1) ' still prints 0
End Sub
If arrays are returned by reference, why doesn't the following work:
'Class1 class module
Private v() As Double
Public Property Get Vec() As Double()
Vec = v()
End Property
Private Sub Class_Initialize()
ReDim v(0 To 3)
End Sub
' end class module
Sub Test1()
Dim c As Class1
Set c = New Class1
Debug.Print c.Vec()(1) ' prints 0 as expected
c.Vec()(1) = 5.6
Debug.Print c.Vec()(1) ' still prints 0
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你没有出租房产。此外,get 属性返回整个数组,而不仅仅是相关元素。将 Property Get 的返回类型从 Double() 更改为普通 Double。添加物业出租。请注意,它需要两个输入,但只传递一个输入。假定最后一个变量(在本例中为 MyValue)从 = 符号后面的内容获取其值。在 Test1() 早期的某个位置放置一个断点,并查看“局部变量”窗口中的值如何受到影响。比较原始代码与我的代码创建的变量:
You don't have a let property. Also, the get property is returning the entire array, rather than just the element in question. Change the return type of Property Get from Double() to just plain Double. Add Property Let. Note that it takes two inputs, but only one is passed to it. The last variable (MyValue, in this case) is assumed to get it's value from whatever is after the = sign. Put a break point somewhere early in Test1() and see how the values are affected in the Locals window. Compare the variables created by the original code versus my code:
在 VBA 中,数组永远不会通过引用返回,除非它们是通过
ByRef
参数返回的。此外,每当您使用=
将数组分配给变量时,您都会创建该数组的新副本,即使您将其分配给过程内的 ByRef 参数,因此您想要让这项工作成功,我几乎不走运。一些替代方法是...
In VBA, arrays are never returned by reference unless they are returned through a
ByRef
parameter. Furthermore, whenever you use=
to assign an array to a variable, you've made a new copy of the array, even if you're assigning it to a ByRef argument inside of a procedure, so you're pretty much out of luck trying to make this work.Some alternative are...
我想建议另一种好方法,使用
Collection
和static Property
来完成此操作,而无需使用类:假设您希望将
xlCVError
枚举作为数组(或集合),例如在出现错误时循环遍历它并根据实际错误进行处理。以下内容在访问时初始化一次:
将其转换为数组或将其实现为数组很简单,但集合对我来说似乎更有用,其缺点是它们的元素不是隐式类型化/(编译时-)类型已检查。
因此,这将例如将其转换为 (只读)数组(具有其他答案/评论中提到的 in-mem-copy-disadvantage):
因此,将示例从 Clayton S的答案到使用一些数组的静态、可修改的模块属性,它将是:
I want to suggest another nice way to do this using a
Collection
and astatic Property
without the need to use a class:imagine you want to have the
xlCVError
enum as an array (or collection), e.g. to loop through it on errors and handle it based on the actual error.The following is initialized once on access:
Turning this into an array or implementing it as an array is straight forward, but collections seem to be more useful to me, with the disadvantage that their elements are not implicitely typed/(compile-time-)type checked.
So this would e.g. turn it into an (read-only) array (with the in-mem-copy-disadvantage mentioned in other answers/comments):
So transforming the example from Clayton Ss answer into a static, modifiable module property using some array it would be: