I am creating an Immutable Linked List class in VBA. It provides ToArray and ToCollection methods, which I have both verified as working. However the Get NewEnum() As IUnknown property is not working and I don't know why.
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = ToCollection.[_NewEnum]
End Property
Stepping through the following code with sequence as an SList with the debugger
Public Function Copy(ByVal sequence As Variant) As SList
Dim made As New SList
Dim element As Variant
For Each element In sequence
Set made = made.Cons(element)
Next
Set Copy = made.Reverse
End Function
shows the For Each element In sequence calling Get NewEnum which builds the collection properly and then returns to Copy and exits the loop after performing no iterations and no errors. My only guess is that NewEnum is an iterator to a variable that is being destroyed when it exits Get NewEnum.
Is that what is happening?
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = ToCollection.[_NewEnum]
End Property
ToCollection is returning a new collection every time it's called; this would probably work:
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Static internalCollection As Collection
If internalCollection Is Nothing Then Set internalCollection = ToCollection
Set NewEnum = internalCollection.[_NewEnum]
End Property
...but it's rather ugly. Ideally you'd have some instance-level encapsulated As Collection to return the [_NewEnum] value from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With