Skip to content

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

@paul1peng

Description

@paul1peng

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions