DKenny
Jan 5th, 2006, 02:21 PM
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
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
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
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
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
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
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
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