asp.net.ph

Array.CreateInstance Method

System Namespace   Array Class


Initializes a new instance of the Array class.

Overload List

1. Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.

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

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

4. Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

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


Example

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

NOTE: This example shows how to use one of the overloaded versions of CreateInstance. For other examples that might be available, see the individual overload topics.

[ 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 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