VBA - 产权返回数组获取 [英] VBA - Returning array from Property Get

查看:198
本文介绍了VBA - 产权返回数组获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数组是通过引用返回,为什么不了以下工作:

 'Class1的类模块
私人V()为双
公共属性获取VEC()为双()
    VEC = V()
高端物业
私人小组Class_Initialize()
    使用ReDim V(0到3)
结束小组
'端类模块Test1的子()
    尺寸C为1级
    集合C =新1级
    Debug.Print c.Vec()(1)'打印0作为预期
    c.Vec()(1)= 5.6
    Debug.Print c.Vec()(1)还是打印0
结束小组


解决方案

在VBA中,数组永远不会被引用,除非他们通过的ByRef 参数返回返回。此外,只要您使用 = 来分配一个数组变量,你所做的数组的一个新副本,即使你它的内部分配给一个ByRef参数一个程序,让你多是pretty运气努力使这项工作。

一些替代是...


  • 使用一个VBA.Collection,而不是一个数组。

  • 请你自己的类,它封装阵列,并公开程序间接访问和处理内部数组。

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

解决方案

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...

  • Use a VBA.Collection instead of an array.
  • Make your own class that encapsulates an array and exposes procedures for indirectly accessing and manipulating the internal array.

这篇关于VBA - 产权返回数组获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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