Hashtable: Clear

Clear

Removes all elements from the Hashtable.



 Public Sub Clear ( )

Remarks

Count is set to zero, and references to other objects from elements of the collection are also released. The capacity remains unchanged.

Examples

The following example shows how to clear the values of the Hashtable.

Public Sub Main()
    ' Creates and initializes a new Hashtable.
    Dim myHT As New Hashtable
    myHT.Add "one", "The"
    myHT.Add "two", "quick"
    myHT.Add "three", "brown"
    myHT.Add "four", "fox"
    myHT.Add "five", "jumped"

    ' Displays the count and values of the Hashtable.
    Debug.Print "Initially,"
    Debug.Print CorString.Format("   Count    : {0}", myHT.Count)
    Debug.Print "   Values:"
    PrintKeysAndValues myHT

    ' Clears the Hashtable.
    myHT.Clear

    ' Displays the count and values of the Hashtable.
    Debug.Print "After Clear,"
    Debug.Print CorString.Format("   Count    : {0}", myHT.Count)
    Debug.Print "   Values:"
    PrintKeysAndValues myHT
End Sub

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

' This example code produces the following output.
'
' Initially,
'    Count    : 5
' Values:
'     -KEY-   -VALUE-
' one:            The
' two:            quick
' four:           fox
' five:           jumped
' three:          brown
'
' After Clear,
'    Count    : 0
' Values:
'     -KEY-   -VALUE-

See Also

Project CorLib Overview

Class Hashtable Overview