VBA Dictionary.Exists(key)方法始终为True

时间:2014-01-10 16:13:15

标签: vba dictionary

编辑: 问题可能与这一行有关:

stationUnits.Add newTag.Unit, New Collection '[Dictionary].Add key:=[String], item:=[Collection]

我遇到了Scripting.Dictionary.Exists(key)方法的问题,拒绝评估为false,因此我可以预测是否需要为它创建新的集合。有谁看到我错过了什么?

有趣的观点:
  - Let ID属性中的MsgBox打印为FALSE
  - 调试器显示newTag.Unit的正确值(字符串)
  - 我已经尝试直接在比较中使用.Exists(而不是调用hasUnit属性),没有区别。
  - 起初我以为.Exists根本就没有工作,但通过.hasUnit属性,我正在观察它从FALSE变为TRUE。字典中还没有添加任何键。 ......为什么要这样做?

''StationData Class
Private stationID As String, stationUnits As Scripting.Dictionary

Public Property Let ID(id_string As String)
    stationID = id_string
    Set stationUnits = New Scripting.Dictionary
    stationUnits.CompareMode = BinaryCompare
    MsgBox stationUnits.Exists("blah")
End Property
Public Property Get ID() As String: ID = stationID: End Function
Public Property Get nUnits(): nUnits = stationUnits.Count: End Property

Public Function addTag(ByVal newTag As TagString)
    If Not newTag.isValid Then
        Exit Function
    End If
    If Not newTag.Station = Me.ID Then
        Exit Function
    End If

    If Not Me.hasUnit(newTag.Unit) Then
    'If the unit key has not already been added, add it along with a new collection
        stationUnits.Add newTag.Unit, New Collection
        stationUnits.Item(newTag.Unit).Add (newTag)
    Else
    'If the unit key already exists, add a new item to its collection.
        stationUnits.Item(newTag.Unit).Add (newTag)
    End If
End Function

Public Property Get hasUnit(ByVal uString As String) As Boolean
    hasUnit = stationUnits.Exists(uString)
End Property

0 个答案:

没有答案