Check if dictionary key is numeric

128 Views Asked by At

I have code from a project that looks at options and prices of used cars. It contains a Dictionary of Dictionaries called "DictOfDictMakeModel".

Several "For Each" loops use "IsNumeric" to look for numeric keys in "DictOfDictMakeModel" Keys collection. Finding one, the key is used in reading options from the same Dictionary. This worked until a few days ago.

Debugging I found some keys "IsNumeric" returned as TRUE were strings, but my Dictionary was expecting LONGs.

I fixed the problem but was wondering if there is a better way?

'Function returns letter IDs of options that are packages, I.E. "Premium Package w/power seats"
Function MSTRpakLtrsCOMRTN() As String
'Return letters of selected Master Package Options
    Dim vKey As Variant
    Dim xKey As Long            'Implicit cast to LONG

'GOOD CODE
    For Each vKey In DictOfDictMakeModel(carModel).Keys
        On Error Resume Next
        xKey = vKey
        If Err.Number = 0 Then
            With DictOfDictMakeModel(carModel)(xKey)
                If .NumOrdered > 0 And .MasterPACKname <> "" Then
                    MSTRpakLtrsCOMRTN = MSTRpakLtrsCOMRTN & .MasterPACKname
                End If
            End With
        End If
        On Error GoTo 0
    Next vKey

'BAD CODE
'    For Each vKey In DictOfDictMakeModel(carModel).Keys
'        If IsNumeric(vKey) Then             'Skip non-numeric keys
'            With DictOfDictMakeModel(carModel)(vKey)
'                If .NumOrdered > 0 And .MasterPACKname <> "" Then
'                    MSTRpakLtrsCOMRTN = MSTRpakLtrsCOMRTN & .MasterPACKname
'                End If
'            End With
'        End If
'    Next vKey
End Function
0

There are 0 best solutions below