Hashtable: Remove

Remove

Removes the element with the specified key from the Hashtable.



 Public Sub Remove(
	  ByRef Key As Variant )

Parameters

Key
[ByRef] Variant. The key of the element to remove.

Remarks

If the Hashtable does not contain an element with the specified key, the Hashtable remains unchanged. No exception is thrown.

This method is an O(1) operation.

Examples

The following example shows how to remove elements from the Hashtable.

Public Sub Main()
    ' Creates and initializes a new Hashtable.
    Dim MyHT As New Hashtable
        
    MyHT.Add "1a", "The"
    MyHT.Add "1b", "quick"
    MyHT.Add "1c", "brown"
    MyHT.Add "2a", "fox"
    MyHT.Add "2b", "jumped"
    MyHT.Add "2c", "over"
    MyHT.Add "3a", "the"
    MyHT.Add "3b", "lazy"
    MyHT.Add "3c", "dog"

    ' Displays the Hashtable.
    Debug.Print "The Hashtable initially contains the following:"
    PrintKeysAndValues MyHT

    ' Removes the element with the key "3b".
    MyHT.Remove "3b"

    ' Displays the current state of the Hashtable.
    Debug.Print "After removing ""lazy"":"
    PrintKeysAndValues MyHT
End Sub

Public Sub PrintKeysAndValues(ByVal MyHT As Hashtable)
    Dim de As DictionaryEntry
    
    For Each de In MyHT
        Debug.Print CorString.Format(t("\t{0}:\t{1}"), de.Key, de.Value)
    Next de
    
    Debug.Print
End Sub

' This example code produces the following output.
'
'   The Hashtable initially contains the following:
'       2a: fox
'       2b: jumped
'       2c: over
'       3a: the
'       3b: lazy
'       3c: dog
'       1a: The
'       1b: quick
'       1c: brown
'
'   After removing "lazy":
'       2a: fox
'       2b: jumped
'       2c: over
'       3a: the
'       3c: dog
'       1a: The
'       1b: quick
'       1c: brown

See Also

Project CorLib Overview

Class Hashtable Overview

Add