Array.CreateInstance Method (Type, Int32, Int32)

 

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function CreateInstance (
	elementType As Type,
	length1 As Integer,
	length2 As Integer
) As Array

Parameters

elementType
Type: System.Type

The Type of the Array to create.

length1
Type: System.Int32

The size of the first dimension of the Array to create.

length2
Type: System.Int32

The size of the second dimension of the Array to create.

Return Value

Type: System.Array

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

Exception Condition
ArgumentNullException

elementType is null.

ArgumentException

elementType is not a valid Type.

NotSupportedException

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

ArgumentOutOfRangeException

length1 is less than zero.

-or-

length2 is less than zero.

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

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of length1 and length2.

The following code example shows how to create and initialize a two-dimensional 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 

.NET Framework
Available since 1.1
Return to top
Show: