Type と次元の長さを指定して、0 から始まるインデックス番号を持つ 2 次元の Array を作成します。
Overloads Public Shared Function CreateInstance( _
ByVal elementType As Type, _ ByVal length1 As Integer, _ ByVal length2 As Integer _) As Array
[C#]
public static Array CreateInstance(TypeelementType,intlength1,intlength2);
[C++]
public: static Array* CreateInstance(Type* elementType,intlength1,intlength2);
[JScript]
public static function CreateInstance(
elementType : Type,length1 : int,length2 : int) : Array;
パラメータ
戻り値
指定した Type の、次元ごとに指定した長さの、0 から始まるインデックス番号を持つ新しい 2 次元の Array 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | elementType が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | elementType が有効な Type ではありません。 |
NotSupportedException | elementType はサポートされていません。 |
ArgumentOutOfRangeException | length1 が 0 未満です。
または length2 が 0 未満です。 |
解説
ほとんどのクラスとは異なり、 Array は、遅延バインディングによるアクセスを可能にするために、パブリック コンストラクタではなく CreateInstance メソッドを用意しています。
参照型の要素は、 null 参照 (Visual Basic では Nothing) に初期化されます。値型の要素は 0 に初期化されます。
使用例
2 次元 Array を作成および初期化する方法を次のコード例に示します。
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a two-dimensional Array of type String.
Dim my2DArray As Array = Array.CreateInstance(GetType(String), 2, 3)
Dim i, j As Integer
For i = my2DArray.GetLowerBound(0) To my2DArray.GetUpperBound(0)
For j = my2DArray.GetLowerBound(1) To my2DArray.GetUpperBound(1)
my2DArray.SetValue("abc" + i.ToString() + j.ToString(), i, j)
Next j
Next i
' Displays the values of the Array.
Console.WriteLine("The two-dimensional Array contains the " _
+ "following values:")
PrintValues(my2DArray)
End Sub
Public Shared Sub PrintValues(myArr As Array)
Dim myEnumerator As System.Collections.IEnumerator = _
myArr.GetEnumerator()
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The two-dimensional Array contains the following values:
' abc00 abc01 abc02
' abc10 abc11 abc12
[C#]
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a two-dimensional Array of type String.
Array my2DArray=Array.CreateInstance( typeof(String), 2, 3 );
for ( int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++ )
for ( int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++ )
my2DArray.SetValue( "abc" + i + j, i, j );
// Displays the values of the Array.
Console.WriteLine( "The two-dimensional Array contains the following values:" );
PrintValues( my2DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The two-dimensional Array contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
[C++]
#using <mscorlib.dll>
using namespace System;
void PrintValues( Array* myArr );
void main() {
// Creates and initializes a two-dimensional Array instance of type String.
Array* my2DArray = Array::CreateInstance( __typeof(String), 2, 3 );
for ( int i = my2DArray->GetLowerBound(0); i <= my2DArray->GetUpperBound(0); i++ )
for ( int j = my2DArray->GetLowerBound(1); j <= my2DArray->GetUpperBound(1); j++ )
my2DArray->SetValue( String::Concat(S"abc", __box(i),__box(j)), i, j ); //concatenation of the string with the int values
// Displays the values of the Array.
Console::WriteLine( "The two-dimensional Array instance contains the following values:" );
PrintValues( my2DArray );
}
void PrintValues( Array* myArr ) {
System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator();
int i = 0;
int cols = myArr->GetLength( myArr->Rank - 1 );
while ( myEnumerator->MoveNext() ) {
if ( i < cols ) {
i++;
}
else {
Console::WriteLine();
i = 1;
}
Console::Write( "\t{0}", myEnumerator->Current );
}
Console::WriteLine();
}
/*
This code produces the following output.
The two-dimensional Array instance contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
[JScript]
import System;
// Creates and initializes a two-dimensional Array of type String.
var my2DArray : System.Array = System.Array.CreateInstance( System.String, 2, 3 );
for ( var i : int = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++ )
for ( var j : int = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++ )
my2DArray.SetValue( "abc" + i + j, i, j );
// Displays the values of the Array.
Console.WriteLine( "The two-dimensional Array contains the following values:" );
PrintValues( my2DArray );
function PrintValues( myArr : System.Array) {
var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
var i : int = 0;
var cols : int = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
/*
This code produces the following output.
The two-dimensional Array contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
Array クラス | Array メンバ | System 名前空間 | Array.CreateInstance オーバーロードの一覧