'Is IsNumeric the best way to check Dictionary Key if it is numeric
I have a small bit of 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 great until a few days ago when it broke.
Debugging I found that the problem was some keys that "IsNumeric" returned as TRUE were strings, but my Dictionary was expecting LONGs.
I fixed problem but was wondering if my fix is a good one. Or does someone have a better one?
I've included my fixed code and the problem code.
Thanks,
'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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
