|
-
Jan 5th, 2006, 03:21 PM
#1
Thread Starter
Frenzied Member
[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:
Sub TryTests()
Dim MyTests As cTests
Dim i As Integer
Dim MyTest As cTest
Set MyTests = New cTests
For i = 1 To 5
MyTests.Add "Test" & CStr(i)
Next i
'This loop structure doesn't work
For Each MyTest In MyTests
Debug.Print MyTest.Name
Next MyTest
Set MyTest = Nothing
Set MyTests = Nothing
End Sub
"cTest" Class Code
VB Code:
Private pNAME As String
Property Get Name() As String
Name = pNAME
End Property
Property Let Name(nName As String)
pNAME = nName
End Property
"cTests" Collection Class Code
VB Code:
Private pCol As Collection
Private Sub Class_Initialize()
Set pCol = New Collection
End Sub
Private Sub Class_Terminate()
Set pCol = Nothing
End Sub
Function Add(TestName As String) As cTest
Dim NewItem As New cTest
NewItem.Name = TestName
pCol.Add Item:=NewItem, key:=TestName
Set Add = NewItem
Set NewItem = Nothing
End Function
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Jan 5th, 2006, 03:51 PM
#2
Thread Starter
Frenzied Member
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:
Sub TryTests()
Dim MyTests As cTests
Dim i As Integer
Dim MyTest As cTest
Set MyTests = New cTests
For i = 1 To 5
MyTests.Add "Test" & CStr(i)
Next i
'This loop structure works
For i = 1 To 5
Debug.Print MyTests.Item(i).Name
Next i
Set MyTest = Nothing
Set MyTests = Nothing
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 
-
Jan 6th, 2006, 10:57 PM
#3
Fanatic Member
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:
Function NewEnum() As IUnknown
' allows iteration of the collection items
Set NewEnum = pCol.[_NewEnum]
End Function
-
Jan 9th, 2006, 11:49 AM
#4
Thread Starter
Frenzied Member
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?
 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 
-
Jan 9th, 2006, 12:04 PM
#5
Thread Starter
Frenzied Member
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 
-
Jan 9th, 2006, 03:13 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|