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.

Blackhawk
  • 5,984
  • 4
  • 27
  • 56
JayC667
  • 2,418
  • 2
  • 17
  • 31
  • [**this**](http://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba/19379641#19379641) 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? –  Nov 25 '13 at 14:00
  • 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. – JayC667 Nov 27 '13 at 20:28

2 Answers2

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.

Community
  • 1
  • 1
RubberDuck
  • 11,933
  • 4
  • 50
  • 95
  • 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. – JayC667 Aug 29 '14 at 17:34
  • 3
    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 IUnknown` – Erik A Mar 08 '19 at 11:13
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.

Tomasz
  • 343
  • 2
  • 9
  • 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. – JayC667 Sep 24 '18 at 09:36