Results 1 to 6 of 6

Thread: [RESOLVED] For Each with custom collection class

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Resolved [RESOLVED] For Each with custom collection class

    If I have created a custom collection class which encapsulates a standard Collection object, how then can I enumerate its members by using For Each?

    e.g.
    VB Code:
    1. Dim objWindow  As CWindow   ' Custom class
    2. Dim objWindows As CWindows  ' Custom collection class
    3.  
    4. objWindows.GetWindows       ' Populate the collection
    5.  
    6. ' Now how can I provide support for this:
    7. For Each objWindow In objWindows
    8.     ' ....
    9. Next objWindow

    I think it works with a NewEnum method or something, but they are all hidden. Any ideas?

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: For Each with custom collection class

    The code for NewEnum to enable For...Each statements on a collection is

    VB Code:
    1. Public Property Get NewEnum() As IUnknown
    2.     Set NewEnum = [I]Name of Collection Variable[/I].[_NewEnum]
    3. End Property

    You must also set the Procedure Attribute for this property. Just copying and pasting the above code will not work.

    Open the Class module
    Tools -> Procedure Attributes menu
    In the Name: combobox select NewEnum
    Click the Advanced button
    In the Procedure Id ComboBox type in -4
    Optionally check Hide This Member.

    If you open the class module in Notepad you should see something like

    VB Code:
    1. Public Property Get NewEnum() As IUnknown
    2. Attribute NewEnum.VB_UserMemId = -4
    3. Attribute NewEnum.VB_MemberFlags = "40"
    4.     Set NewEnum = Name of Collection variable.[_NewEnum]
    5. End Property

  3. #3

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: For Each with custom collection class

    Sweet! Thanks mate

  4. #4

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] For Each with custom collection class

    Just out of interest, what if I didn't have a collection variable and was instead doing it manually. Does the NewEnum method return the next item in the collection each time it is called? If so, what does it return when you run out of items?

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] For Each with custom collection class

    what if I didn't have a collection variable and was instead doing it manually.
    For...Each works on arrays as well. Not sure I understand what you mean by "doing it manually".

    Does the NewEnum method return the next item in the collection each time it is called?
    Yes.

    If so, what does it return when you run out of items?
    The For...Each loop just exits. You would need the code the "Compiler" generates to figure out how it implements For...Each. As to what it returns, most likely Null or Nothing.

  6. #6

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] For Each with custom collection class

    I found a page on it. Thanks for your help

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