SortedList: SetByIndex

SetByIndex

Replaces the value at a specific index in a SortedList object.



 Public Sub SetByIndex(
	  ByVal Index As Long,
	  ByRef Value As Variant )

Parameters

Index
[ByVal] Long. The zero-based index at which to set the value.
Value
[ByRef] Variant. The value to save into the SortedList object.

Remarks

The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.

Exceptions

ExceptionCondition
ArgumentOutOfRangeException Index is outside the range of valid indexes for the SortedList object.

Examples

The following code example shows how to replace the value of an existing element in a SortedList object.

Public Sub Main()
    Dim List As New SortedList

    List.Add 2, "two"
    List.Add 3, "three"
    List.Add 1, "one"
    List.Add 0, "zero"
    List.Add 4, "four"
    
    Debug.Print "The SortedList contains the following values:"
    PrintIndexAndKeysAndValues List
    
    List.SetByIndex 3, "III"
    List.SetByIndex 4, "IV"
    
    Debug.Print "After replacing the value at index 3 and index 4,"
    PrintIndexAndKeysAndValues List
End Sub

Private Sub PrintIndexAndKeysAndValues(ByVal List As SortedList)
    Dim i As Long
    
    Debug.Print t("\t-INDEX-\t-KEY-\t-VALUE-")
    
    For i = 0 To List.Count - 1
        Debug.Print CorString.Format(t("\t[{0}]:\t{1}\t{2}"), i, List.GetKey(i), List.GetByIndex(i))
    Next
    
    Debug.Print
End Sub
 
' The following code produces the following output.
'
'    The SortedList contains the following values:
'        -INDEX- -KEY-   -VALUE-
'        [0]:    0   zero
'        [1]:    1   one
'        [2]:    2   two
'        [3]:    3   three
'        [4]:    4   four
'
'    After replacing the value at index 3 and index 4,
'        -INDEX- -KEY-   -VALUE-
'        [0]:    0   zero
'        [1]:    1   one
'        [2]:    2   two
'        [3]:    3   III
'        [4]:    4   IV

See Also

Project CorLib Overview

Class SortedList Overview