How to implement For Each Loop in Collection


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to implement For Each Loop in Collection

  1. #1
    sameers Guest

    How to implement For Each Loop in Collection


    Is there anyone who can give me a sample of implementing For Each Loop on
    User created Collection?

    Thanks

    Sameers
    Sameers@theangrycoder.com

  2. #2
    Paul Clement Guest

    Re: How to implement For Each Loop in Collection

    On 18 Dec 2001 21:25:12 GMT, "sameers" <sameers@theangrycoder.com> wrote:


    Is there anyone who can give me a sample of implementing For Each Loop on
    User created Collection?


    Relatively simply example:

    Dim col As New Collection
    Dim vItem As Variant

    col.Add "Test"
    col.Add "Another Test"
    col.Add "Yet Another Test"

    For Each vItem In col
    Debug.Print vItem
    Next

    Set col = Nothing


    Paul ~~~ pclement@ameritech.net
    Microsoft MVP (Visual Basic)

  3. #3
    Raghu Guest

    Re: How to implement For Each Loop in Collection


    Hi Sameer,
    You need to implement a Public Function in your Collection Class like as
    below
    ________
    ' NewEnum must return the IUnknown interface of a
    ' collection's enumerator.
    Public Function NewEnum() As IUnknown
    Set NewEnum = m_colObjects.[_NewEnum]
    End Function
    ___________
    where m_colObjects is your private collection class object declared as
    ___________
    Private m_colObjects as Collection
    ___________
    And hide the NewEnum method and give it a procedure ID -4
    To do that, go to Tools menu, click Procedure Attributes to open the Procedure
    Attributes dialog box. In Name box, select the NewEnum method.

    Click Advanced to show the advanced features. Check Hide this member to make
    NewEnum hidden in the type library.

    In the Procedure ID box, type 4 (minus four) to give NewEnum the procedure
    ID required by For Each ... Next. Click OK.

    Now you collection class will be capable of providing For Each ... Next
    functionality.

    HTH,
    Raghu.



    Paul Clement <UseAdddressAtEndofMessage@swspectrum.com> wrote:
    >On 18 Dec 2001 21:25:12 GMT, "sameers" <sameers@theangrycoder.com> wrote:
    >
    >
    > Is there anyone who can give me a sample of implementing For Each Loop

    on
    > User created Collection?
    >
    >
    >Relatively simply example:
    >
    >Dim col As New Collection
    >Dim vItem As Variant
    >
    >col.Add "Test"
    >col.Add "Another Test"
    >col.Add "Yet Another Test"
    >
    >For Each vItem In col
    > Debug.Print vItem
    >Next
    >
    >Set col = Nothing
    >
    >
    >Paul ~~~ pclement@ameritech.net
    >Microsoft MVP (Visual Basic)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center