How to implement custom iterable class in VBA

随声附和 提交于 2019-11-27 03:27:31

问题


I want to add a feature to my classes so I can use them in for-each loops.

I wrote hashmaps, arraylists, queues, sets and so on that I want to iterate over. Now I'm looking for a way to implement the IUnknown class to build custom iterators.

I already know how to use

private objPeople as Collection
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    Set NewEnum = objPeople.[_NewEnum]
End Property

but all those examples out there are based on the Collection class, which I do not want to use.

What I want to focus on is trying to implement the IUnknown interface, but I haven't found any references on how to do that.

I have vast experience in Java, C++, C# and so on, so I assume that THERE HAS TO BE A WAY to implement that even in VBA, maybe even with API calls stuff like that.


回答1:


The answer is that it can be done, but it is ugly (requires and IDL, two .BAS modules and two .cls modules (one of which is your Collection Class module). For full information see this link:

Create Your Own "Super Collections" in VB

Good luck! It seemed to complicated for what I needed to do, so for now I just iterate over the Collection object enumerator.




回答2:


The long and short of it is you can't implement IUnknown. The same goes for Collection. They're both Com objects that are unavailable for extension in VBA. You can create custom collections and do other very cool iterable things though.



来源:https://stackoverflow.com/questions/20194919/how-to-implement-custom-iterable-class-in-vba

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