SortedList: GetKey

GetKey

Returns the key at the specified index in the list.



 Public Function GetKey(
	  ByVal Index As Long ) As Variant

Parameters

Index
[ByVal] Long. The index into the list from which to retrieve the key.

Return Values

Variant -  The key at the specified index.

Remarks

Since the list maintains sort order, when an item is added or removed, there is no garauntee that the same key will be found at the same index in future calls.

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