Me again. I'm not trying to persecute you, just testing VBA-Dictionary against Scripting.Dictionary to see if it'll do what I need.
Scripting.Dictionary's behaviour when passed boolean True or False as a key is pretty stupid. The key is stored as a boolean (see Typename() calls in code below) but compared as a number so -1 will collide with True and 0 with False. Similar snafu with dates, btw. I think this a bug, given that the documentation suggests the key can be of any type.
VBA-Dictionary by contrast handles as one would expect: True and -1 are different, as are False and 0. I haven't tested it with date keys.
Sub T4()
Dim SD As Object
Dim VD As Dictionary
Dim K As Variant
Set SD = CreateObject("Scripting.Dictionary")
Set VD = New Dictionary
SD.Add True, "Boolean True"
SD.Add False, "Boolean False"
On Error Resume Next
SD.Add -1, "Minus One"
Debug.Print "SD: " & Err.Number ' Error 457
Err.Clear
SD.Add 0, "Zero"
Debug.Print "SD: " & Err.Number ' Error 457
Err.Clear
On Error GoTo 0
For Each K In SD.Keys
Debug.Print K, TypeName(K), SD(K) ' Prints as expected
Next
VD.Add True, "Boolean True"
VD.Add False, "Boolean False"
On Error Resume Next
VD.Add -1, "Minus One"
Debug.Print "VD: " & Err.Number ' No error
Err.Clear
VD.Add 0, "Zero"
Debug.Print "VD: " & Err.Number ' No error
Err.Clear
On Error GoTo 0
For Each K In VD.Keys
Debug.Print K, TypeName(K), VD(K) 'Both items have the same value.
Next
End Sub
@JNurick no worries, my goal is complete compatibility so finding these edge cases is great. This is definitely the weirdest of the bunch, but this will be fixed along with the other key issues shortly.
Me again. I'm not trying to persecute you, just testing VBA-Dictionary against Scripting.Dictionary to see if it'll do what I need.
Scripting.Dictionary's behaviour when passed boolean True or False as a key is pretty stupid. The key is stored as a boolean (see Typename() calls in code below) but compared as a number so -1 will collide with True and 0 with False. Similar snafu with dates, btw. I think this a bug, given that the documentation suggests the key can be of any type.
VBA-Dictionary by contrast handles as one would expect: True and -1 are different, as are False and 0. I haven't tested it with date keys.