VBA Dictionary.Exists (key) is always True

EDIT: Probably the problem is with this line:

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

I am having a problem with the Scripting.Dictionary.Exists (key) method, which refuses to evaluate to false so that I can anticipate whether I need to create a new collection for it. Does anyone see what I'm missing?

Interesting points:
 - The MsgBox in the Let ID property prints FALSE
 - The debugger shows the correct value (string) for newTag.Unit
 - I tried using .Exists directly in comparison (instead of calling the hasUnit property), there is no difference.
 - At first I thought: “Exists” does not work at all, but through the .hasUnit property I watch how it changes from FALSE to TRUE. By the way, keys have not been added yet .... why does he do this?

''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
+1

Source: https://habr.com/ru/post/1693994/


All Articles