This documentation is archived and is not being maintained.

NameObjectCollectionBase.BaseSet Method

Sets the value of an entry in the NameObjectCollectionBase instance.

Overload List

Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Protected Sub BaseSet(Integer, Object)
[C#] protected void BaseSet(int, object);
[C++] protected: void BaseSet(int, Object*);
[JScript] protected function BaseSet(int, Object);

Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Protected Sub BaseSet(String, Object)
[C#] protected void BaseSet(string, object);
[C++] protected: void BaseSet(String*, Object*);
[JScript] protected function BaseSet(String, Object);

Example

[Visual Basic, C#, C++] The following code example uses BaseSet to set the value of a specific element.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of BaseSet. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class MyCollection
   Inherits NameObjectCollectionBase

   ' Gets or sets the value at the specified index.
   Default Public Property Item(index As Integer) As [Object]
      Get
         Return Me.BaseGet(index)
      End Get
      Set
         Me.BaseSet(index, value)
      End Set
   End Property

   ' Gets or sets the value associated with the specified key.
   Default Public Property Item(key As [String]) As [Object]
      Get
         Return Me.BaseGet(key)
      End Get
      Set
         Me.BaseSet(key, value)
      End Set
   End Property

   ' Gets a String array that contains all the keys in the collection.
   Public ReadOnly Property AllKeys() As [String]()
      Get
         Return Me.BaseGetAllKeys()
      End Get
   End Property

   ' Adds elements from an IDictionary into the new collection.
   Public Sub New(d As IDictionary)
      Dim de As DictionaryEntry
      For Each de In  d
         Me.BaseAdd(CType(de.Key, [String]), de.Value)
      Next de
   End Sub 'New

End Class 'MyCollection


Public Class SamplesNameObjectCollectionBase   

   Public Shared Sub Main()

      ' Creates and initializes a new MyCollection instance.
      Dim d = New ListDictionary()
      d.Add("red", "apple")
      d.Add("yellow", "banana")
      d.Add("green", "pear")
      Dim myCol As New MyCollection(d)
      Console.WriteLine("Initial state of the collection:")
      PrintKeysAndValues2(myCol)
      Console.WriteLine()

      ' Sets the value at index 1.
      myCol(1) = "sunflower"
      Console.WriteLine("After setting the value at index 1:")
      PrintKeysAndValues2(myCol)
      Console.WriteLine()

      ' Sets the value associated with the key "red".
      myCol("red") = "tulip"
      Console.WriteLine("After setting the value associated with the key ""red"":")
      PrintKeysAndValues2(myCol)

   End Sub 'Main

   Public Shared Sub PrintKeysAndValues2(myCol As MyCollection)
      Dim s As [String]
      For Each s In  myCol.AllKeys
         Console.WriteLine("{0}, {1}", s, myCol(s))
      Next s
   End Sub 'PrintKeysAndValues2

End Class 'SamplesNameObjectCollectionBase 


'This code produces the following output.
'
'Initial state of the collection:
'red, apple
'yellow, banana
'green, pear
'
'After setting the value at index 1:
'red, apple
'yellow, sunflower
'green, pear
'
'After setting the value associated with the key "red":
'red, tulip
'yellow, sunflower
'green, pear


[C#] 
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyCollection : NameObjectCollectionBase  {

   // Gets or sets the value at the specified index.
   public Object this[ int index ]  {
      get  {
         return( this.BaseGet( index ) );
      }
      set  {
         this.BaseSet( index, value );
      }
   }

   // Gets or sets the value associated with the specified key.
   public Object this[ String key ]  {
      get  {
         return( this.BaseGet( key ) );
      }
      set  {
         this.BaseSet( key, value );
      }
   }

   // Gets a String array that contains all the keys in the collection.
   public String[] AllKeys  {
      get  {
         return( this.BaseGetAllKeys() );
      }
   }

   // Adds elements from an IDictionary into the new collection.
   public MyCollection( IDictionary d )  {
      foreach ( DictionaryEntry de in d )  {
         this.BaseAdd( (String) de.Key, de.Value );
      }
   }

}

public class SamplesNameObjectCollectionBase  {

   public static void Main()  {

      // Creates and initializes a new MyCollection instance.
      IDictionary d = new ListDictionary();
      d.Add( "red", "apple" );
      d.Add( "yellow", "banana" );
      d.Add( "green", "pear" );
      MyCollection myCol = new MyCollection( d );
      Console.WriteLine( "Initial state of the collection:" );
      PrintKeysAndValues2( myCol );
      Console.WriteLine();

      // Sets the value at index 1.
      myCol[1] = "sunflower";
      Console.WriteLine( "After setting the value at index 1:" );
      PrintKeysAndValues2( myCol );
      Console.WriteLine();

      // Sets the value associated with the key "red".
      myCol["red"] = "tulip";
      Console.WriteLine( "After setting the value associated with the key \"red\":" );
      PrintKeysAndValues2( myCol );

   }

   public static void PrintKeysAndValues2( MyCollection myCol )  {
      foreach ( String s in myCol.AllKeys )  {
         Console.WriteLine( "{0}, {1}", s, myCol[s] );
      }
   }

}


/*
This code produces the following output.

Initial state of the collection:
red, apple
yellow, banana
green, pear

After setting the value at index 1:
red, apple
yellow, sunflower
green, pear

After setting the value associated with the key "red":
red, tulip
yellow, sunflower
green, pear

*/

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

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

public __gc class MyCollection : public NameObjectCollectionBase
{
public:
   // Gets or sets the value at the specified index.
   __property Object* get_Item( int index )
   {
      return(this->BaseGet(index));
   }
   __property void set_Item( int index, Object* value )
   {
      this->BaseSet(index, value);
   }

   // Gets or sets the value associated with the specified key.
   __property Object* get_Item( String* key )
   {
      return(this->BaseGet(key));
   }
   __property void set_Item( String* key, Object* value )
   {
      this->BaseSet(key, value);
   }

   // Gets a String array that contains all the keys in the collection.
   __property String* get_AllKeys()[]
   {
      return(this->BaseGetAllKeys());
   }

   // Adds elements from an IDictionary* into the new collection.
   MyCollection(IDictionary* d)
   {
      IEnumerator* myEnum = d->GetEnumerator();
      while (myEnum->MoveNext())
      {
         DictionaryEntry* de = __try_cast<DictionaryEntry*>(myEnum->Current);
         this->BaseAdd(__try_cast<String*>(de->Key), de->Value);
      }
   }
};

void PrintKeysAndValues2(MyCollection* myCol) 
{
   IEnumerator* myEnum = myCol->AllKeys->GetEnumerator();
   while (myEnum->MoveNext()) 
   {
      String* s = __try_cast<String*>(myEnum->Current);

      Console::WriteLine(S" {0}, {1}", s, myCol->Item[s]);
   }
}

int main() 
{
   // Creates and initializes a new MyCollection instance.
   IDictionary* d = new ListDictionary();
   d->Add(S"red", S"apple");
   d->Add(S"yellow", S"banana");
   d->Add(S"green", S"pear");
   MyCollection* myCol = new MyCollection(d);
   Console::WriteLine(S"Initial state of the collection:");
   PrintKeysAndValues2(myCol);
   Console::WriteLine();

   // Sets the value at index 1.
   myCol->Item[1] = S"sunflower";
   Console::WriteLine(S"After setting the value at index 1:");
   PrintKeysAndValues2(myCol);
   Console::WriteLine();

   // Sets the value associated with the key "red".
   myCol->Item[S"red"] = S"tulip";
   Console::WriteLine(S"After setting the value associated with the key \"red\":");
      PrintKeysAndValues2(myCol);
}

/*
This code produces the following output.

Initial state of the collection:
red, apple
yellow, banana
green, pear

After setting the value at index 1:
red, apple
yellow, sunflower
green, pear

After setting the value associated with the key "red":
red, tulip
yellow, sunflower
green, pear

*/

[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.

See Also

NameObjectCollectionBase Class | NameObjectCollectionBase Members | System.Collections.Specialized Namespace

Show: