I have a class module setup to create a collection of the same class. My goal is to assign a Key based on the individual items' existing property. However, whenever I try to access an item in the class module collection based on this Key, I get "Run-time error 13 - Type mismatch".
Here is the stripped down code for the class module
:
Option Explicit
Private mstrKey As String
Private mblnChecked As Boolean
Private mtnpsColl As Collection
Private Sub Class_Initialize()
Set mtnpsColl = New Collection
End Sub
Public Sub Add(Object As tnpObject)
mtnpsColl.Add _
Item:=Object, _
Key:=Object.Key
End Sub
'Also tried this alternative with the same results
'Public Sub Add(Object As tnpObject, Key As String)
' mtnpsColl.Add _
' Item:=Object, _
' Key:=Key'
'End Sub
Public Function Item(Index As Integer) As tnpObject
Set Item = mtnpsColl.Item(Index)
End Function
'Also tried declaring Item with Property Get
'Found examples of both Item function and Property Get
'Unclear what the difference is
'Public Property Get Item(Index As Integer) As tnpObject
' Set Item = mtnpsColl.Item(Index)
'End Property
Public Function Count() As Integer
Count = mtnpsColl.Count
End Function
Public Property Let Key(Value As String)
mstrKey = Value
End Property
Public Property Get Key() As String
Key = mstrKey
End Property
Public Property Let Checked(Value As Boolean)
mblnChecked = Value
End Property
Public Property Get Checked() As Boolean
Checked = mblnChecked
End Property
...and here is the test sub to compare how a standard collection and this class module collection function:
Option Explicit
Public Sub TestClsModuleColl()
'Standard collection, the "control group"
Dim objColl As Collection
'Class module collection
Dim tnpsTest As tnpObject
'A couple of items to add to these collections
Dim tnpItem1 As tnpObject
Dim tnpItem2 As tnpObject
Dim i As Integer
'Set and populate both collections with these simple items
Set objColl = New Collection
Set tnpsTest = New tnpObject
Set tnpItem1 = New tnpObject
Set tnpItem2 = New tnpObject
With tnpItem1
.Key = "something true"
.Checked = True
End With
objColl.Add tnpItem1, tnpItem1.Key
tnpsTest.Add tnpItem1
'Also tried this alternative with the same results
' tnpsTest.Add tnpItem1, tnpItem1.Key
With tnpItem2
.Key = "something false"
.Checked = False
End With
objColl.Add tnpItem2, tnpItem2.Key
tnpsTest.Add tnpItem2
'Also tried this alternative with the same results
' tnpsTest.Add tnpItem2, tnpItem2.Key
'No problem accessing standard collection items based on Index
With objColl
For i = 1 To .Count
Debug.Print .Item(i).Key
Debug.Print .Item(i).Checked
Next i
End With
'No problem accessing standard collection items based on Key
Debug.Print objColl.Item("something true").Key
Debug.Print objColl.Item("something true").Checked
'No problem accessing class module collection items based on Index
With tnpsTest
For i = 1 To .Count
Debug.Print .Item(i).Key
Debug.Print .Item(i).Checked
Next i
'Error when accessing class module collection items based on Key
'Run-time error 13, Type mismatch
Debug.Print .Item("something true").Key
Debug.Print .Item("something true").Checked
End With
End Sub
I get the sense that the standard collection class has more functions/properties to access an Item based on the Key. However, I can only find examples on how to setup a collection within the class module as shown above. Is there more lines of code I need to add to the Item function? Does it matter if Item is decclared as a Function vs Property Get? Do I need to change the variable type away from integer? I'm in the dark.
CodePudding user response:
The parameter of your Item-function is an integer, therefore string is not a valid type.
If you want to access it either by index or by key your have to set the parameter to variant.