Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
paul1peng opened this issue May 17, 2021 · 1 comment
Open

Comments

@paul1peng
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants