次の方法で共有


Array.SetValue メソッド (Object, Int32 )

値を多次元 Array 内の指定した位置にある要素に設定します。インデックスは 32 ビット整数値の配列として指定します。

Overloads Public Sub SetValue( _
   ByVal value As Object, _   ByVal ParamArray indices() As Integer _)
[C#]
public void SetValue(objectvalue,   params int[] indices);
[C++]
public: void SetValue(Object* value,intindices __gc[]);
[JScript]
public function SetValue(
   value : Object,indices : int[]);

パラメータ

  • value
    指定した要素の新しい値。
  • indices
    設定する要素の位置を指定するインデックスを表す 32 ビット整数の 1 次元配列。

例外

例外の種類 条件
ArgumentNullException indices が null 参照 (Visual Basic では Nothing) です。
ArgumentException 現在の Array 内の次元数が、 indices 内の要素数と等しくありません。
InvalidCastException value を現在の Array の要素型にキャストできません。
IndexOutOfRangeException indices 内のいずれかの要素が、現在の Array の対応する次元にとって有効なインデックス範囲の外にあります。

解説

indices 内の要素数が、 Array 内の次元数と等しい必要があります。 indices 配列内のすべての要素をまとめた全体で、多次元の Array 内にある目的の要素の位置が指定されている必要があります。

GetLowerBound メソッドと GetUpperBound メソッドを使用して、 indices 配列内に範囲外の値があるかどうかを確認します。

変換の詳細については、 Convert のトピックを参照してください。

使用例

[Visual Basic, C#, C++] 1 次元配列または多次元配列の特定の値を設定および取得する方法を次のコード例に示します。

 
Imports System

Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a one-dimensional array.
      Dim myArr1(4) As [String]

      ' Sets the element at index 3.
      myArr1.SetValue("three", 3)
      Console.WriteLine("[3]:   {0}", myArr1.GetValue(3))


      ' Creates and initializes a two-dimensional array.
      Dim myArr2(5, 5) As [String]

      ' Sets the element at index 1,3.
      myArr2.SetValue("one-three", 1, 3)
      Console.WriteLine("[1,3]:   {0}", myArr2.GetValue(1, 3))


      ' Creates and initializes a three-dimensional array.
      Dim myArr3(5, 5, 5) As [String]

      ' Sets the element at index 1,2,3.
      myArr3.SetValue("one-two-three", 1, 2, 3)
      Console.WriteLine("[1,2,3]:   {0}", myArr3.GetValue(1, 2, 3))


      ' Creates and initializes a seven-dimensional array.
      Dim myArr7(5, 5, 5, 5, 5, 5, 5) As [String]

      ' Sets the element at index 1,2,3,0,1,2,3.
      Dim myIndices() As Integer = {1, 2, 3, 0, 1, 2, 3}
      myArr7.SetValue("one-two-three-zero-one-two-three", myIndices)
      Console.WriteLine("[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue(myIndices))

   End Sub 'Main 

End Class 'SamplesArray


'This code produces the following output.
'
'[3]:   three
'[1,3]:   one-three
'[1,2,3]:   one-two-three
'[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three


[C#] 
using System;

public class SamplesArray  {

   public static void Main()  {
 
      // Creates and initializes a one-dimensional array.
      String[] myArr1 = new String[5];

      // Sets the element at index 3.
      myArr1.SetValue( "three", 3 );
      Console.WriteLine( "[3]:   {0}", myArr1.GetValue( 3 ) );


      // Creates and initializes a two-dimensional array.
      String[,] myArr2 = new String[5,5];

      // Sets the element at index 1,3.
      myArr2.SetValue( "one-three", 1, 3 );
      Console.WriteLine( "[1,3]:   {0}", myArr2.GetValue( 1, 3 ) );


      // Creates and initializes a three-dimensional array.
      String[,,] myArr3 = new String[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3.SetValue( "one-two-three", 1, 2, 3 );
      Console.WriteLine( "[1,2,3]:   {0}", myArr3.GetValue( 1, 2, 3 ) );


      // Creates and initializes a seven-dimensional array.
      String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 };
      myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices );
      Console.WriteLine( "[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue( myIndices ) );

   }
 
}


/* 
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/


[C++] 
#using <mscorlib.dll>
using namespace System;

int main()  {
 
      // Creates and initializes a one-dimensional array.
      String* myArr1[] = new String*[5];

      // Sets the element at index 3.
      myArr1->SetValue( S"three", 3 );
      Console::WriteLine( S"[3]:   {0}", myArr1->GetValue( 3 ) );


      // Creates and initializes a two-dimensional array.
      String* myArr2[,] = new String*[5,5];

      // Sets the element at index 1,3.
      myArr2->SetValue( S"one-three", 1, 3 );
      Console::WriteLine( S"[1,3]:   {0}", myArr2->GetValue( 1, 3 ) );


      // Creates and initializes a three-dimensional array.
      String* myArr3[,,] = new String*[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3->SetValue( S"one-two-three", 1, 2, 3 );
      Console::WriteLine( S"[1,2,3]:   {0}", myArr3->GetValue( 1, 2, 3 ) );


      // Creates and initializes a seven-dimensional array.
      String* myArr7[,,,,,,] = new String*[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      Int32 myIndices[] = { 1, 2, 3, 0, 1, 2, 3 };
      myArr7->SetValue( S"one-two-three-zero-one-two-three", myIndices);
      Console::WriteLine( S"[1,2,3,0,1,2,3]:   {0}", myArr7->GetValue( myIndices ) );

}


/* 
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Array クラス | Array メンバ | System 名前空間 | Array.SetValue オーバーロードの一覧 | GetLowerBound | GetUpperBound | GetValue