Unable to create New Enum in VBA

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!