asp.net.ph

Array.CreateInstance Method ( Type, Int32 [ ] )

System Namespace   Array Class


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

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

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

[ C++ ]
[Serializable]
public: static Array* CreateInstance (
   Type* elementType,
   int lengths __gc [ ]
);

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

Parameters

elementType
The Type of the Array to create.
lengths
An array that contains the size of each dimension of the Array to create.

Return Value

A new multidimensional 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 ).

-or-

lengths is a null reference ( Nothing ).

ArgumentException elementType is not a valid Type.

-or-

The lengths array contains less than one element.

ArgumentOutOfRangeException Any value in lengths is less than zero.

Remarks

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

The number of elements in the lengths array must equal the number of dimensions in the new Array. Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

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 multidimensional Array.

[ VB ] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main ( ) 
        
        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray ( ) As Integer = {2, 3, 4, 5}
        Dim my4DArray As Array = Array.CreateInstance ( GetType ( String ) , myLengthsArray ) 
        Dim i, j, k, l As Integer
        Dim myIndicesArray ( ) As Integer
        For i = my4DArray.GetLowerBound ( 0 ) To my4DArray.GetUpperBound ( 0 ) 
            For j = my4DArray.GetLowerBound ( 1 ) To my4DArray.GetUpperBound ( 1 ) 
                For k = my4DArray.GetLowerBound ( 2 ) To my4DArray.GetUpperBound ( 2 ) 
                    For l = my4DArray.GetLowerBound ( 3 ) To my4DArray.GetUpperBound ( 3 ) 
                        myIndicesArray = New Integer ( ) {i, j, k, l}
                        my4DArray.SetValue ( Convert.ToString ( i ) + j.ToString ( ) _
                           + k.ToString ( ) + l.ToString ( ) , myIndicesArray ) 
                    Next l
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Response.WriteLine ( "The four-dimensional Array contains the following values:" ) 
        PrintValues ( my4DArray ) 
    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 four-dimensional Array contains the following values:
'     0000    0001    0002    0003    0004
'     0010    0011    0012    0013    0014
'     0020    0021    0022    0023    0024
'     0030    0031    0032    0033    0034
'     0100    0101    0102    0103    0104
'     0110    0111    0112    0113    0114
'     0120    0121    0122    0123    0124
'     0130    0131    0132    0133    0134
'     0200    0201    0202    0203    0204
'     0210    0211    0212    0213    0214
'     0220    0221    0222    0223    0224
'     0230    0231    0232    0233    0234
'     1000    1001    1002    1003    1004
'     1010    1011    1012    1013    1014
'     1020    1021    1022    1023    1024
'     1030    1031    1032    1033    1034
'     1100    1101    1102    1103    1104
 '    1110    1111    1112    1113    1114
'     1120    1121    1122    1123    1124
'     1130    1131    1132    1133    1134
'     1200    1201    1202    1203    1204
'     1210    1211    1212    1213    1214
'     1220    1221    1222    1223    1224
'     1230    1231    1232    1233    1234

[ C# ] 
using System;
public class SamplesArray  {

   public static void Main ( )  {

      // Creates and initializes a multidimensional Array of type String.
      int [ ] myLengthsArray = new int[4] { 2, 3, 4, 5 };
      Array my4DArray=Array.CreateInstance ( typeof ( String ) , myLengthsArray );
      for ( int i = my4DArray.GetLowerBound ( 0 ); i <= my4DArray.GetUpperBound ( 0 ); i++ ) 
         for ( int j = my4DArray.GetLowerBound ( 1 ); j <= my4DArray.GetUpperBound ( 1 ); j++ ) 
            for ( int k = my4DArray.GetLowerBound ( 2 ); k <= my4DArray.GetUpperBound ( 2 ); k++ ) 
               for ( int l = my4DArray.GetLowerBound ( 3 ); l <= my4DArray.GetUpperBound ( 3 ); l++ )  {
                  int [ ] myIndicesArray = new int[4] { i, j, k, l };
                  my4DArray.SetValue ( Convert.ToString ( i ) + j + k + l, myIndicesArray );
               }

      // Displays the values of the Array.
      Response.WriteLine ( "The four-dimensional Array contains the following values:" );
      PrintValues ( my4DArray );
   }


   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 four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/

[JScript ] 
import System;

// Creates and initializes a multidimensional Array of type String.
var myLengthsArray : int [ ] = [ 2, 3, 4, 5 ];
var my4DArray : System.Array = System.Array.CreateInstance ( System.String, myLengthsArray );
for ( var i : int = my4DArray.GetLowerBound ( 0 ); i <= my4DArray.GetUpperBound ( 0 ); i++ ) 
  for ( var j : int = my4DArray.GetLowerBound ( 1 ); j <= my4DArray.GetUpperBound ( 1 ); j++ ) 
     for ( var k : int = my4DArray.GetLowerBound ( 2 ); k <= my4DArray.GetUpperBound ( 2 ); k++ ) 
        for ( var l : int = my4DArray.GetLowerBound ( 3 ); l <= my4DArray.GetUpperBound ( 3 ); l++ )  {
           var myIndicesArray : int [ ] = [ i, j, k, l ];
           my4DArray.SetValue ( Convert.ToString ( i ) + j + k + l, myIndicesArray );
        }

// Displays the values of the Array.
Response.WriteLine ( "The four-dimensional Array contains the following values:" );
PrintValues ( my4DArray );
 
 
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 four-dimensional Array contains the following values:
     0000    0001    0002    0003    0004
     0010    0011    0012    0013    0014
     0020    0021    0022    0023    0024
     0030    0031    0032    0033    0034
     0100    0101    0102    0103    0104
     0110    0111    0112    0113    0114
     0120    0121    0122    0123    0124
     0130    0131    0132    0133    0134
     0200    0201    0202    0203    0204
     0210    0211    0212    0213    0214
     0220    0221    0222    0223    0224
     0230    0231    0232    0233    0234
     1000    1001    1002    1003    1004
     1010    1011    1012    1013    1014
     1020    1021    1022    1023    1024
     1030    1031    1032    1033    1034
     1100    1101    1102    1103    1104
     1110    1111    1112    1113    1114
     1120    1121    1122    1123    1124
     1130    1131    1132    1133    1134
     1200    1201    1202    1203    1204
     1210    1211    1212    1213    1214
     1220    1221    1222    1223    1224
     1230    1231    1232    1233    1234
 */
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