i have an issue with dynamic arrays being passed to class byVal instead byRef, so simplified class, cArray
Option Explicit
Private mArray() As String
Public Sub init(ByRef iArray() As String)
mArray = iArray
End Sub
Public Property Get count() As Long
count = UBound(mArray) - LBound(mArray)
End Property
Public Property Get item(iIndex As Long) As String
item = mArray(iIndex)
End Property
and simple function in module
Private Sub arrTest()
Dim arr() As String, cont As cArray
ReDim arr(0 To 1)
arr(0) = "value0"
arr(1) = "value1"
Set cont = New cArray
cont.init arr
arr(1) = "newValue1"
Debug.Print cont.item(1), arr(1) 'will print value1, newValue1 even though is expected to be same
ReDim Preserve arr(0 To 2)
arr(2) = "value2"
Debug.Print cont.count 'will print 1
End Sub
so, question is, is this bug? normal behavior? something else?
Dim x() As Long: someobj.ArrayProperty = x: Erase x. How issomeobjsupposed to know whetherxhas been freed or not? Arrays aren't reference types in VBA, so there isn't really a safe way to determine if the calling code or the object is responsible for cleaning up the array's memory allocation (they aren't reference counted). Dynamic arrays in particular would problematic in this regard because they can beReDim'd.ArraytomArray- that is a by value assignment (ie. it creates a copy of the array inmArrayinstead of creating an additional pointer to the original array you passed in). So, you can make changes toiArrayin theinitSub and those will be reflected inarrTestbut that does not create any link between arr and mArray