implementing "For Each" for custom class
Hello,
Assume I created a class similar to a collection.
This means that it would make sense to iterate on items contained in this class.
Is it possible to write code in VB6 to enable the "For Each ..." construct working with this class?
I have little hopes, but I ask ...
Re: implementing "For Each" for custom class
Re: implementing "For Each" for custom class
Yep, there's a lot you can do with ingenuity. Callback objects for COM callbacks is another one with general applications.
Re: implementing "For Each" for custom class
dilettante,
I have the feeling Microsoft is joking on this topic.
"Creating your own collection" is a misleading way of speaking.
They should have said "decorating VB.Collection".
I have found very few replacements for VB.Collection: CHipe and CTreap.
Simple experimentation shows that it is clearly possible to get better performance and flexibility than what is offerered by VB.Collection.
But this excludes reusing VB.Collection in any way.
I would like to have the new class fully compatible with the VB.Collection.
Unfortunately, none of the two replacement I found are satisfactory:
CTreap is much faster that VB.Collection but not compatible
CHipe is pretty much compatible, faster than VB.Collection, but not as fast as CTreap, and lacks the "For Each" compatibility.
My best option, for the moment, is to interface CTreap.
This is mainly simple mechanics, like converting sub signature, case sensitivity, ...
But the "For Each" question is not yet clear. I will limit myself to VB, and avoid going to C++. I must also stay in the VB6 environment. (in java this could have been solved 100 times, and similarly for MS clone of java)
1 Attachment(s)
Re: implementing "For Each" for custom class
Continuing with source postings... here is ObjectCollection class, it has some degree of compatibility with Collection, but to avoid Variant as much as possible I had to do a few changes:- Item only takes keys (use Object to get Item by index)
- Remove only takes keys (use RemoveIndex to remove by index)
- Only stores objects
I did some feature additions too:- you may change Item/Object unlike in Collection
- you can get the Key by index
- Add, Remove and RemoveIndex return True when they succeed
And of course "For Each" works just fine :) The only major problem is that it hasn't been extensively tested. Another problem is that unlike Collection there are no errors raised where Collection would raise one (so this is not compatible with those folks who use On Error extensively).
Speed in comparison to CTreap and CHipe... I don't know. I did take a look at CTreap and it seemed massive with more class files I could count in one eye sight.