VBA数组按值传递的例子?
我认为数组总是在VBA中通过引用传递,但是这个例子似乎是个例外:
' Class Module "C" Private a_() As Long Public Property Let a(a() As Long) Debug.Print VarPtr(a(0)) a_ = a End Property ' Standard Module Sub test() Dim a() As Long: ReDim a(9) Debug.Print VarPtr(a(0)) ' output: 755115384 Dim oc As C Set oc = New C oc.a = a ' output: 752875104 End Sub
这是窃听我,因为我需要有一个包含一个数组的类,它正在做一个额外的副本。
这似乎工作,至less通过引用传递:
Sub test() Dim oc As New C Dim a() As Long: ReDim a(9) Debug.Print "test: " & VarPtr(a(0)) oc.Set_A a() End Sub
在类模块C
:
Private a_() As Long Public Property Let a(a() As Long) Debug.Print "Let: " & VarPtr(a(0)) a_ = a End Property Function Set_A(a() As Long) Debug.Print "Set_A: " & VarPtr(a(0)) a_ = a End Function
我注意到, a(0)
和oc.a_(0)
的VarPtr
评估是不同的,但我不确定这是否适合您的需求: