How to implement custom iterable class in VBA
Asked Answered
C

2

9

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.

Cholinesterase answered 25/11, 2013 at 13:55 Comment(2)
this is related. See comments section for links to other references. And I'm sorry but I am not understanding what you are actually trying to implement?Demodulation
Yeah they are realted. But nothing that I don't already know. They all get the IUnknown iterator from the Collections class they're using. I want to go one more step and instead of using Collection in my code, I want to write my own "Iterator" (java language). So instead of using "Set NewEnum = objPeople.[_NewEnum]" I want to be able to write "Set NewEnum = new MyIterator: NewEnum.init(Me)" to create an Iterator/Enumeration so I can use foreach on my classes (ArrayLists, HashMaps ets) without having to use the Collection.Cholinesterase
F
7

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.

Fogbound answered 23/9, 2018 at 14:20 Comment(1)
Wow, thanx a lot for this post! Yes, and even if it's quite ugly-hackish, it still is possible. Thats why I select your answer as the solution.Cholinesterase
S
8

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.

Sempiternal answered 27/8, 2014 at 1:12 Comment(2)
Thanx for all the input. Had seen those ideas myself, but none lead to the result I was hoping for: to get away from using Collection.Cholinesterase
Nitpick for people coming from Google and looking to implement IUnknown: You can't not implement IUnknown in VBA. It's the base COM interface. Every VBA class implements IUnknown. You can test this by using TypeOf SomeClass Is IUnknownCrassus
F
7

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.

Fogbound answered 23/9, 2018 at 14:20 Comment(1)
Wow, thanx a lot for this post! Yes, and even if it's quite ugly-hackish, it still is possible. Thats why I select your answer as the solution.Cholinesterase

© 2022 - 2024 — McMap. All rights reserved.