Type と次元の長さを指定して、0 から始まるインデックス番号を持つ 3 次元の Array を作成します。
Overloads Public Shared Function CreateInstance( _
ByVal elementType As Type, _ ByVal length1 As Integer, _ ByVal length2 As Integer, _ ByVal length3 As Integer _) As Array
[C#]
public static Array CreateInstance(TypeelementType,intlength1,intlength2,intlength3);
[C++]
public: static Array* CreateInstance(Type* elementType,intlength1,intlength2,intlength3);
[JScript]
public static function CreateInstance(
elementType : Type,length1 : int,length2 : int,length3 : int) : Array;
パラメータ
- elementType
作成する Array の Type 。 - length1
作成する Array の最初の次元のサイズ。 - length2
作成する Array の 2 番目の次元のサイズ。 - length3
作成する Array の 3 番目の次元のサイズ。
戻り値
指定した Type の、次元ごとに指定した長さの、0 から始まるインデックス番号を持つ新しい 3 次元の Array 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | elementType が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | elementType が有効な Type ではありません。 |
NotSupportedException | elementType はサポートされていません。 |
ArgumentOutOfRangeException | length1 が 0 未満です。
または length2 が 0 未満です。 または length3 が 0 未満です。 |
解説
ほとんどのクラスとは異なり、 Array は、遅延バインディングによるアクセスを可能にするために、パブリック コンストラクタではなく CreateInstance メソッドを用意しています。
参照型の要素は、 null 参照 (Visual Basic では Nothing) に初期化されます。値型の要素は 0 に初期化されます。
使用例
3 次元 Array を作成および初期化する方法を次のコード例に示します。
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a three-dimensional Array of type Object.
Dim my3DArray As Array = Array.CreateInstance(GetType(Object), 2, 3, 4)
Dim i As Integer
For i = my3DArray.GetLowerBound(0) To my3DArray.GetUpperBound(0)
Dim j As Integer
For j = my3DArray.GetLowerBound(1) To my3DArray.GetUpperBound(1)
Dim k As Integer
For k = my3DArray.GetLowerBound(2) To my3DArray.GetUpperBound(2)
my3DArray.SetValue("abc" + i.ToString() _
+ j.ToString() + k.ToString(), i, j, k)
Next k
Next j
Next i
' Displays the values of the Array.
Console.WriteLine("The three-dimensional Array contains the " _
+ "following values:")
PrintValues(my3DArray)
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 three-dimensional Array contains the following values:
' abc000 abc001 abc002 abc003
' abc010 abc011 abc012 abc013
' abc020 abc021 abc022 abc023
' abc100 abc101 abc102 abc103
' abc110 abc111 abc112 abc113
' abc120 abc121 abc122 abc123
[C#]
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a three-dimensional Array of type Object.
Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
my3DArray.SetValue( "abc" + i + j + k, i, j, k );
// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
}
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 three-dimensional Array contains the following values:
abc000 abc001 abc002 abc003
abc010 abc011 abc012 abc013
abc020 abc021 abc022 abc023
abc100 abc101 abc102 abc103
abc110 abc111 abc112 abc113
abc120 abc121 abc122 abc123
*/
[C++]
#using <mscorlib.dll>
using namespace System;
void PrintValues( Array* myArr );
void main() {
// Creates and initializes a three-dimensional Array instance of type Object.
Array* my3DArray = Array::CreateInstance( __typeof(Object), 2, 3, 4 );
for ( int i = my3DArray->GetLowerBound(0); i <= my3DArray->GetUpperBound(0); i++ )
for ( int j = my3DArray->GetLowerBound(1); j <= my3DArray->GetUpperBound(1); j++ )
for ( int k = my3DArray->GetLowerBound(2); k <= my3DArray->GetUpperBound(2); k++ )
my3DArray->SetValue( String::Concat( S"abc", __box(i), __box(j), __box(k)), i, j, k );
// Displays the values of the Array.
Console::WriteLine( "The three-dimensional Array instance contains the following values:" );
PrintValues( my3DArray );
}
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 three-dimensional Array instance contains the following values:
abc000 abc001 abc002 abc003
abc010 abc011 abc012 abc013
abc020 abc021 abc022 abc023
abc100 abc101 abc102 abc103
abc110 abc111 abc112 abc113
abc120 abc121 abc122 abc123
*/
[JScript]
import System;
// Creates and initializes a three-dimensional Array of type Object.
var my3DArray : System.Array = System.Array.CreateInstance( System.Object, 2, 3, 4 );
for ( var i : int = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
for ( var j : int = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
for ( var k : int = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
my3DArray.SetValue( "abc" + i + j + k, i, j, k );
// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
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 three-dimensional Array contains the following values:
abc000 abc001 abc002 abc003
abc010 abc011 abc012 abc013
abc020 abc021 abc022 abc023
abc100 abc101 abc102 abc103
abc110 abc111 abc112 abc113
abc120 abc121 abc122 abc123
*/
必要条件
プラットフォーム: 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 オーバーロードの一覧