Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create New Enum in VBA

Tags:

vba

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?

like image 795
cheezsteak Avatar asked Nov 03 '14 18:11

cheezsteak


1 Answers

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.

like image 97
Mathieu Guindon Avatar answered Sep 23 '22 07:09

Mathieu Guindon