asp.net.ph

Array.CreateInstance Method ( Type, Int32, Int32, Int32 )

System Namespace   Array Class


Creates a three-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

[ VB ]
<Serializable>
Overloads Public Shared Function CreateInstance ( _
   ByVal elementType As Type, _
   ByVal length1 As Integer, _
   ByVal length2 As Integer, _
   ByVal length3 As Integer _
) As Array

[ C# ]
[Serializable]
public static Array CreateInstance (
   Type elementType,
   int length1,
   int length2,
   int length3
);

[ C++ ]
[Serializable]
public: static Array* CreateInstance (
   Type* elementType,
   int length1,
   int length2,
   int length3
);

[JScript ]
public Serializable
static function CreateInstance (
   elementType : Type,
   length1 : int,
   length2 : int,
   length3 : int
) : Array;

Parameters

elementType
The Type of the Array to create.
length1
The size of the first dimension of the Array to create.
length2
The size of the second dimension of the Array to create.
length3
The size of the third dimension of the Array to create.

Return Value

A new three-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exceptions


Exception Type Condition
ArgumentNullException elementType is a null reference ( Nothing in Visual Basic ).
ArgumentException elementType is not a valid Type.
ArgumentOutOfRangeException length1 is less than zero.

-or-

length2 is less than zero.

-or-

length3 is less than zero.


Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to a null reference ( Nothing in Visual Basic ). Value-type elements are initialized to zero.

Example

The following code example shows how to create and initialize a three-dimensional Array.

[ VB ] 
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.
        Response.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
                Response.WriteLine ( ) 
                i = 1
            End If
            Response.Write ( ControlChars.Tab + "{0}", myEnumerator.Current ) 
        End While
        Response.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.
      Response.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  {
            Response.WriteLine ( );
            i = 1;
         }
         Response.Write ( "\t{0}", myEnumerator.Current );
      }
      Response.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
*/

[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.
Response.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  {
         Response.WriteLine ( );
         i = 1;
      }
      Response.Write ( "\t{0}", myEnumerator.Current );
   }
   Response.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
 */
See Also

Array Members   Array.CreateInstance Overload List Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph