12

I've been placed on a project whose client front end is written in VB 6, ack! I'm trying to develop a custom collection class that supports the For...Each syntax. Is this possible in VB 6? Or am I stuck with using the For..Next with counter to identify the index.

Thanks for the help!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Chris
  • 123
  • 1
  • 5
  • 1
    what can I say, gots to pay the mortgage, I'm hoping VB6 devs will become rare like those Cobol devs, then when M$ pulls the plug on the VB 6 runtime, I'll be there to cash in on all the migrations to .net...unless, I've already missed that boat, dang! – Chris Apr 12 '11 at 02:08

1 Answers1

20

The key part is adding this to the custom collection class...

Public Function NewEnum() As IUnknown
    Set NewEnum = m_Employees.[_NewEnum]
End Function

and in the procedure attributes, set the procedure id to -4, like so: enter image description here

Ref: http://www.vb-helper.com/howto_custom_collection_with_for_each.html

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Justin Largey
  • 1,934
  • 1
  • 16
  • 21
  • 1
    It feels wrong to upvote anything related to VB6, but very good answer. :-) – Samuel Neff Apr 12 '11 at 02:01
  • I'm upvoting your comment, too funny! I know what you mean, feel bad for Chris – Justin Largey Apr 12 '11 at 02:02
  • 3
    Because there is `#define DISPID_NEWENUM (-4)` in `colldispid.h` along with `#define DISPID_LISTITEM 0` and `#define DISPID_LISTCOUNT (-531)` but these are not used by VB's `For Each` operator. – wqw Apr 12 '11 at 06:53