Results 1 to 6 of 6

Thread: implementing "For Each" for custom class

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    21

    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 ...

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: implementing "For Each" for custom class


  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    SelfEnum

    Here is my shot at no Collection solution, meaning a two class module solution: the one is for the item and the other is the actual class. SelfCallback is such a wonderful thing to exist!

    The class is very simple, it just has minimal Add, Count and Item properties. All the other code is dedicated for the enumeration effect to have the support for For Each without a Collection.


    Quote Originally Posted by MSDN
    You can't write an enumerator object with Visual Basic
    MSDN is wrong
    Attached Files Attached Files

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    21

    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)

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.
    Attached Files Attached Files
    Last edited by Merri; Jul 17th, 2008 at 04:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width