Results 1 to 6 of 6

Thread: [RESOLVED] For Each...Next Loop - User Defined Class and Collection - Excel2005

  1. #1

    Thread Starter
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Resolved [RESOLVED] For Each...Next Loop - User Defined Class and Collection - Excel2005

    I am trying to use the For Each...Next looping structure with a User Defined Class and a corresponding User defined Collection Class.
    From the Help files on F E...N -
    "For collections, element can only be a Variant variable, a generic object variable, or any specific object variable. "
    Based on this, I assumed I would be able to use this structure with UD classes, but I can't make it happen. I keep getting the "Object doesn't support this property or method (Error 438)" error.

    Here's a slimmed down version of my class, collection and a code snippet demonstrating the error.

    As always - any help is appreciated.

    Sample Module Code
    VB Code:
    1. Sub TryTests()
    2. Dim MyTests As cTests
    3. Dim i As Integer
    4. Dim MyTest As cTest
    5.    
    6.     Set MyTests = New cTests
    7.     For i = 1 To 5
    8.         MyTests.Add "Test" & CStr(i)
    9.     Next i
    10.    
    11.     'This loop structure doesn't work
    12.     For Each MyTest In MyTests
    13.         Debug.Print MyTest.Name
    14.     Next MyTest
    15.    
    16.     Set MyTest = Nothing
    17.     Set MyTests = Nothing
    18. End Sub

    "cTest" Class Code
    VB Code:
    1. Private pNAME As String
    2.  
    3. Property Get Name() As String
    4.     Name = pNAME
    5. End Property
    6.  
    7. Property Let Name(nName As String)
    8.     pNAME = nName
    9. End Property

    "cTests" Collection Class Code
    VB Code:
    1. Private pCol As Collection
    2.  
    3. Private Sub Class_Initialize()
    4.     Set pCol = New Collection
    5. End Sub
    6.  
    7. Private Sub Class_Terminate()
    8.     Set pCol = Nothing
    9. End Sub
    10.  
    11. Function Add(TestName As String) As cTest
    12. Dim NewItem As New cTest
    13.    
    14.     NewItem.Name = TestName
    15.    
    16.     pCol.Add Item:=NewItem, key:=TestName
    17.     Set Add = NewItem
    18.    
    19.     Set NewItem = Nothing
    20. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  2. #2

    Thread Starter
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: For Each...Next Loop - User Defined Class and Collection - Excel2005

    PS
    I know that I can use a For..Next loop, as below, but the reason for my post is to understand why the other apporach isn't working. I'm fairly new to user defined classes and collections and want to understand if its something in my approach that is preventing the use of the For Each... approach.

    VB Code:
    1. Sub TryTests()
    2. Dim MyTests As cTests
    3. Dim i As Integer
    4. Dim MyTest As cTest
    5.    
    6.     Set MyTests = New cTests
    7.     For i = 1 To 5
    8.         MyTests.Add "Test" & CStr(i)
    9.     Next i
    10.    
    11.     'This loop structure works
    12.     For i = 1 To 5
    13.         Debug.Print MyTests.Item(i).Name
    14.     Next i
    15.  
    16.    
    17.     Set MyTest = Nothing
    18.     Set MyTests = Nothing
    19. End Sub

    Thanks
    Declan
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: For Each...Next Loop - User Defined Class and Collection - Excel2005

    Is the Add method the only function in cTests?

    In order to traverse the collection, you'll need another function in cTests to enumerate the data items:
    VB Code:
    1. Function NewEnum() As IUnknown
    2. ' allows iteration of the collection items
    3.     Set NewEnum = pCol.[_NewEnum]
    4. End Function

  4. #4

    Thread Starter
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: For Each...Next Loop - User Defined Class and Collection - Excel2005

    I've added the code as suggested, but am still getting the 438 error.
    Any ideas why?
    Quote Originally Posted by ZeBula8
    Is the Add method the only function in cTests?
    No, I just removed everthing else in order to focus on the issue. In the real code there are many more properties and functions in the collection class.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: For Each...Next Loop - User Defined Class and Collection - Excel2005

    OK, I did a Google search on " NewEnum VBA" and found the following. (Mods: this is a post in another forum, not sure if its acceptable to post it here, if not I will remove)
    NewEnum Function

    Basically, you need to export, then delete, the collection class and add the following line into the "NewEnum" function using a text editor.
    Attribute NewEnum.VB_UserMemId = -4

    After that you just re-import the class and you can use For Each...

    ZeBula8 - thanks for pointing me in the right direction.
    Last edited by DKenny; Jan 9th, 2006 at 03:14 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] For Each...Next Loop - User Defined Class and Collection - Excel2005

    (Mods: this is a post in another forum, not sure if its acceptable to post it here, if not I will remove)
    As it's the answer to the thread question, it's fine

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