VBA-tools / VBA-Dictionary

Drop-in replacement for Scripting.Dictionary on Mac
MIT License
348 stars 89 forks source link

Removing an object key shifts values of all object keys added after it #32

Open paul1peng opened 3 years ago

paul1peng commented 3 years ago

Given a dictionary with multiple object keys, if I remove one of these keys, then the values associated with the object keys added after the removed one "shift down", with the value of the object key added immediately after the removed key becoming Empty. See:

Public Sub DictionaryTest()
    Dim dict As New Dictionary
    Dim key1 As New Dictionary 'or any other initialized object
    Dim key2 As New Dictionary 'or any other initialized object
    Dim key3 As New Dictionary 'or any other initialized object
    dict(key1) = 1
    dict(key2) = 2
    dict(key3) = 3

    Debug.Print dict(key3) = 3 'prints True
    Debug.Print dict(key3) = 2 'prints False
    Debug.Print dict(key2) = 2 'prints True
    Debug.Print IsEmpty(dict(key2)) 'prints False

    dict.Remove key1

    Debug.Print dict(key3) = 3 'prints False!
    Debug.Print dict(key3) = 2 'prints True!
    Debug.Print dict(key2) = 2 'prints False!
    Debug.Print IsEmpty(dict(key2)) 'prints True!
End Sub

I think that this is because of how dictionaries internally map object keys to a string representation based on their index in dict_pObjectKeys. When one object is removed from this collection, the indices of the other objects automatically shift down, changing their string representations and resulting in this bug.

cristianbuse commented 5 months ago

This repository does not seem to be maintained. Alternatively use VBA-FastDictionary which is well tested and faster