无法使用密钥访问 class 模块集合,"Run-time error 13 - Type mismatch"

Can't access class module collection with Key, "Run-time error 13 - Type mismatch"

我有一个 class 模块设置来创建相同 class 的集合。我的目标是根据各个项目的现有 属性 分配密钥。但是,每当我尝试基于此密钥访问 class 模块集合中的项目时,我都会收到“运行-time error 13 - Type mismatch”。

这是 class 模块的精简代码 :

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

...这里是比较标准集合和这个 class 模块集合功能的测试子:

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

我觉得标准集合 class 有更多 functions/properties 可以根据键访问项目。但是,我只能找到有关如何在 class 模块中设置集合的示例,如上所示。是否需要向 Item 函数添加更多代码行? Item 声明为 Function 与 属性 Get 有关系吗?我是否需要将变量类型从整数更改为其他类型?我在黑暗中。

您的 Item-function 的参数是一个整数,因此 string 不是一个有效的类型。

如果您想通过索引或按键访问它,您必须将参数设置为 variant。