Export (0) Print
Expand All

SortedList.GetByIndex Method

Gets the value at the specified index of the SortedList.

[Visual Basic]
Public Overridable Function GetByIndex( _
   ByVal index As Integer _
) As Object
[C#]
public virtual object GetByIndex(
 int index
);
[C++]
public: virtual Object* GetByIndex(
 int index
);
[JScript]
public function GetByIndex(
   index : int
) : Object;

Parameters

index
The zero-based index of the value to get.

Return Value

The value at the specified index of the SortedList.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException index is outside the range of valid indexes for the SortedList.

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 removed, the indexing also adjusts accordingly. Therefore, the index of a specific key-and-value pair might change as elements are added or removed from the SortedList.

Example

[Visual Basic, C#, C++] The following example shows how to get one or all the keys or values in the SortedList.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesSortedList
        
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(1.3, "fox")
        mySL.Add(1.4, "jumped")
        mySL.Add(1.5, "over")
        mySL.Add(1.2, "brown")
        mySL.Add(1.1, "quick")
        mySL.Add(1.0, "The")
        mySL.Add(1.6, "the")
        mySL.Add(1.8, "dog")
        mySL.Add(1.7, "lazy")
        
        ' Gets the key and the value based on the index.
        Dim myIndex As Integer = 3
        Console.WriteLine("The key   at index {0} is {1}.", myIndex, _
           mySL.GetKey(myIndex))
        Console.WriteLine("The value at index {0} is {1}.", myIndex, _
           mySL.GetByIndex(myIndex))
        
        ' Gets the list of keys and the list of values.
        Dim myKeyList As IList = mySL.GetKeyList()
        Dim myValueList As IList = mySL.GetValueList()
        
        ' Prints the keys in the first column and the values in the second column.
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To mySL.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}" & ControlChars.Tab & _
               "{1}", myKeyList(i), myValueList(i))
        Next i
    End Sub
End Class

' This 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

[C#] 
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 1.3, "fox" );
      mySL.Add( 1.4, "jumped" );
      mySL.Add( 1.5, "over" );
      mySL.Add( 1.2, "brown" );
      mySL.Add( 1.1, "quick" );
      mySL.Add( 1.0, "The" );
      mySL.Add( 1.6, "the" );
      mySL.Add( 1.8, "dog" );
      mySL.Add( 1.7, "lazy" );

      // Gets the key and the value based on the index.
      int myIndex=3;
      Console.WriteLine( "The key   at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );
      Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );

      // Gets the list of keys and the list of values.
      IList myKeyList = mySL.GetKeyList();
      IList myValueList = mySL.GetValueList();

      // Prints the keys in the first column and the values in the second column.
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < mySL.Count; i++ )
         Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] );
   }
}
/* 
This 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
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

int main()  {

   // Creates and initializes a new SortedList.
   SortedList* mySL = new SortedList();
   mySL->Add( __box(1.3), S"fox" );
   mySL->Add( __box(1.4), S"jumped" );
   mySL->Add( __box(1.5), S"over" );
   mySL->Add( __box(1.2), S"brown" );
   mySL->Add( __box(1.1), S"quick" );
   mySL->Add( __box(1.0), S"The" );
   mySL->Add( __box(1.6), S"the" );
   mySL->Add( __box(1.8), S"dog" );
   mySL->Add( __box(1.7), S"lazy" );

   // Gets the key and the value based on the index.
   int myIndex=3;
   Console::WriteLine( S"The key   at index {0} is {1}.", __box(myIndex), mySL->GetKey( myIndex ) );
   Console::WriteLine( S"The value at index {0} is {1}.", __box(myIndex), mySL->GetByIndex( myIndex ) );

   // Gets the list of keys and the list of values.
   IList* myKeyList = mySL->GetKeyList();
   IList* myValueList = mySL->GetValueList();

   // Prints the keys in the first column and the values in the second column.
   Console::WriteLine( S"\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < mySL->Count; i++ )
      Console::WriteLine( S"\t{0}\t{1}", myKeyList->Item[i], myValueList->Item[i] );
}

/*
This 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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortedList Class | SortedList Members | System.Collections Namespace | IndexOfKey | IndexOfValue

Show:
© 2014 Microsoft