2

EDIT: The problem is probably linked to this line:

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

I'm having a problem with the Scripting.Dictionary.Exists(key) method refusing to evaluate as false so that I can anticipate whether or not 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've tried using .Exists directly in the comparison (instead of calling the hasUnit property), there's no difference.
- At first I thought .Exists was not working at all, but through the .hasUnit property I'm watching it change from FALSE to TRUE. No keys have been added to the dictionary yet. ...why is it doing 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
Neat Machine
  • 681
  • 4
  • 11
  • 18
  • 2
    You can check the content of the dictionary in the locals window while debugging with F8. Do you see the key available for the item you are checking for existence? – html_programmer Jan 10 '14 at 16:53
  • The locals window is only showing an "Item" member for my dictionary, it shows "Item1" as having the value that should have been used as the Key. Not sure if Item1 is referring to the first key anyway and it's just a naming conflict with the debugger and dictionary type. – Neat Machine Jan 10 '14 at 17:03
  • In retrospect, Item1 must refer to a key since .Exists is now passing. So I guess the real issue is that I'm not adding the item associated with that key correctly. This would be the line: stationUnits.Add newTag.Unit, New Collection – Neat Machine Jan 10 '14 at 17:04
  • 1
    Alternatively, you can use debug.print to print all keys and items in the dictionary (using a loop) before checking the "exists" method... – html_programmer Jan 10 '14 at 17:25

0 Answers0