我认为数组总是在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
这让我烦恼,因为我需要一个包含数组的类,并且它需要额外的副本。
答案 0 :(得分:0)
这似乎有效,至少在参考文献中通过:
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
我注意到VarPtr
和a(0)
的{{1}}评价不同,我不确定这是否适合您的需求: