NameObjectCollectionBase.BaseSet 方法

定义

设置实例中 NameObjectCollectionBase 条目的值。

重载

名称 说明
BaseSet(Int32, Object)

设置实例的指定索引处的 NameObjectCollectionBase 条目的值。

BaseSet(String, Object)

如果找到,则设置实例中 NameObjectCollectionBase 具有指定键的第一个条目的值;否则,将具有指定键和值的条目添加到实例中 NameObjectCollectionBase

示例

下面的代码示例用于 BaseSet 设置特定元素的值。

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

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

End Class


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

   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

End Class


'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

BaseSet(Int32, Object)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

设置实例的指定索引处的 NameObjectCollectionBase 条目的值。

protected:
 void BaseSet(int index, System::Object ^ value);
protected void BaseSet(int index, object value);
protected void BaseSet(int index, object? value);
member this.BaseSet : int * obj -> unit
Protected Sub BaseSet (index As Integer, value As Object)

参数

index
Int32

要设置的项的从零开始的索引。

value
Object

表示 Object 要设置的条目的新值。 该值可以是 null

例外

集合为只读。

index 超出集合的有效索引范围。

注解

此方法是 O(1) 操作。

另请参阅

适用于

BaseSet(String, Object)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

如果找到,则设置实例中 NameObjectCollectionBase 具有指定键的第一个条目的值;否则,将具有指定键和值的条目添加到实例中 NameObjectCollectionBase

protected:
 void BaseSet(System::String ^ name, System::Object ^ value);
protected void BaseSet(string name, object value);
protected void BaseSet(string? name, object? value);
member this.BaseSet : string * obj -> unit
Protected Sub BaseSet (name As String, value As Object)

参数

name
String

String要设置的项的键。 键可以是 null

value
Object

表示 Object 要设置的条目的新值。 该值可以是 null

例外

集合为只读。

注解

如果集合包含具有指定键的多个条目,则此方法仅设置第一个条目。 若要设置具有相同键的后续条目的值,请使用枚举器循环访问集合并比较键。

此方法是 O(1) 操作。

另请参阅

适用于