SortedList: GetByIndex

GetByIndex

Gets the value at the specified index of a SortedList object.



 Public Function GetByIndex(
	  ByVal Index As Long ) As Variant

Parameters

Index
[ByVal] Long. The zero-based index of the value to get.

Return Values

Variant -  The value at the specified index in 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 get one or all the keys or values in a SortedList object.

Public Sub Main()
    Dim List As New SortedList
    Dim KeyList As IList
    Dim ValueList As IList
    Dim Index As Long
    Dim i As Long
    
    List.Add 1.3, "fox"
    List.Add 1.4, "jumped"
    List.Add 1.5, "over"
    List.Add 1.2, "brown"
    List.Add 1.1, "quick"
    List.Add 1#, "The"
    List.Add 1.6, "the"
    List.Add 1.8, "dog"
    List.Add 1.7, "lazy"
    
    Index = 3
    Debug.Print CorString.Format("The key   at index {0} is {1}.", Index, List.GetKey(Index))
    Debug.Print CorString.Format("The value at index {0} is {1}.", Index, List.GetByIndex(Index))
    
    Set KeyList = List.GetKeyList
    Set ValueList = List.GetValueList
    
    Debug.Print , "Key", "Value"
    
    For i = 0 To List.Count - 1
        Debug.Print , KeyList(i), ValueList(i)
    Next
End Sub

' The code produces the following output.
'
'    The key   at index 3 is 1.3.
'    The value at index 3 is fox.
'      Key           Value
'       1            The
'       1.1          quick
'       1.2          brown
'       1.3          fox
'       1.4          jumped
'       1.5          over
'       1.6          the
'       1.7          lazy
'       1.8          dog

See Also

Project CorLib Overview

Class SortedList Overview

IndexOfKey

IndexOfValue