SortedList: TrimToSize

TrimToSize

Sets the capacity to the actual number of elements in a SortedList object.



 Public Sub TrimToSize ( )

Remarks

This method can be used to minimize a collections memory overhead if no new elements will be added to the collection.

To reset a SortedList object to its initial state, call the Clear method before calling TrimToSize. Trimming an empty SortedList sets the capacity of the SortedList to the default capacity.

Exceptions

ExceptionCondition
NotSupportedException The SortedList object is read-only.
-or-
The SortedList is fixed size.

Examples

The following code example shows how to trim the unused portions of a SortedList object and how to clear its values.

Public Sub Main()
    Dim List As New SortedList

    List.Add "one", "The"
    List.Add "two", "quick"
    List.Add "three", "brown"
    List.Add "four", "fox"
    List.Add "five", "jumped"
    
    Debug.Print "Initially,"
    Debug.Print CorString.Format("   Count    : {0}", List.Count)
    Debug.Print CorString.Format("   Capacity : {0}", List.Capacity)
    Debug.Print CorString.Format("   Values:")
    PrintKeysAndValues List
    
    List.TrimToSize
    
    Debug.Print "After TrimToSize,"
    Debug.Print CorString.Format("   Count    : {0}", List.Count)
    Debug.Print CorString.Format("   Capacity : {0}", List.Capacity)
    Debug.Print CorString.Format("   Values:")
    PrintKeysAndValues List
    
    List.Clear
    
    Debug.Print "After Clear,"
    Debug.Print CorString.Format("   Count    : {0}", List.Count)
    Debug.Print CorString.Format("   Capacity : {0}", List.Capacity)
    Debug.Print CorString.Format("   Values:")
    PrintKeysAndValues List
    
    List.TrimToSize
    
    Debug.Print "After the second TrimToSize,"
    Debug.Print CorString.Format("   Count    : {0}", List.Count)
    Debug.Print CorString.Format("   Capacity : {0}", List.Capacity)
    Debug.Print CorString.Format("   Values:")
    PrintKeysAndValues List
End Sub

Private Sub PrintKeysAndValues(ByVal List As SortedList)
    Dim i As Long
    
    Debug.Print t("\t-KEY-\t-VALUE-")
    
    For i = 0 To List.Count - 1
        Debug.Print CorString.Format(t("\t{0}:\t{1}"), List.GetKey(i), List.GetByIndex(i))
    Next
    
    Debug.Print
End Sub

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

See Also

Project CorLib Overview

Class SortedList Overview

Clear

Capacity

Count