Export (0) Print
Expand All

SortedList.SetByIndex Method

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

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual void SetByIndex (
	int index,
	Object value
)
public void SetByIndex (
	int index, 
	Object value
)
public function SetByIndex (
	index : int, 
	value : Object
)
Not applicable.

Parameters

index

The zero-based index at which to save value.

value

The Object to save into the SortedList object. The value can be a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentOutOfRangeException

index is outside the range of valid indexes for the SortedList object.

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.

This method is an O(1) operation.

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

using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 2, "two" );
      mySL.Add( 3, "three" );
      mySL.Add( 1, "one" );
      mySL.Add( 0, "zero" );
      mySL.Add( 4, "four" );

      // Displays the values of the SortedList.
      Console.WriteLine( "The SortedList contains the following values:" );
      PrintIndexAndKeysAndValues( mySL );

      // Replaces the values at index 3 and index 4.
      mySL.SetByIndex( 3, "III" );
      mySL.SetByIndex( 4, "IV" );

      // Displays the updated values of the SortedList.
      Console.WriteLine( "After replacing the value at index 3 and index 4," );
      PrintIndexAndKeysAndValues( mySL );
   }


   public static void PrintIndexAndKeysAndValues( SortedList myList )  {
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) );
      }
      Console.WriteLine();
   }
}
/* 
This 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
*/ 

import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[] args)
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();

        mySL.Add((Int32)2, "two");
        mySL.Add((Int32)3, "three");
        mySL.Add((Int32)1, "one");
        mySL.Add((Int32)0, "zero");
        mySL.Add((Int32)4, "four");

        // Displays the values of the SortedList.
        Console.WriteLine("The SortedList contains the following values:");
        PrintIndexAndKeysAndValues(mySL);

        // Replaces the values at index 3 and index 4.
        mySL.SetByIndex(3, "III");
        mySL.SetByIndex(4, "IV");

        // Displays the updated values of the SortedList.
        Console.WriteLine("After replacing the value at index 3 and index 4,");
        PrintIndexAndKeysAndValues(mySL);
    } //main

    public static void PrintIndexAndKeysAndValues(SortedList myList)
    {
        Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.get_Count(); i++) {
            Console.WriteLine("\t[{0}]:\t{1}\t{2}", 
                System.Convert.ToString(i), 
                System.Convert.ToString(myList.GetKey(i)), 
                System.Convert.ToString(myList.GetByIndex(i)));
        }
        Console.WriteLine();
    } //PrintIndexAndKeysAndValues

} //SamplesSortedList

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft