SortedList: GetByIndex |
Gets the value at the specified index of a SortedList object.
Public Function GetByIndex( ByVal Index As Long ) As Variant
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.
Exception | Condition |
---|---|
ArgumentOutOfRangeException | Index is outside the range of valid indexes for the SortedList object. |
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