The first thing to note about accessing .NET arrays is that, by default, .NET array dimensions use zero (0)-based indexing as compared to ABL arrays, which always use one (1)-based indexing.

CAUTION: This means that when working together with ABL and .NET arrays, ABL arrays are always indexed from 1 to the array size while .NET array dimensions are typically indexed from 0 to the dimension size minus 1.
Note: .NET allows you to specify the base index value for dimensions of any .NET array that you create. However, in practice, almost all .NET arrays are created to use the zero-based index default.

You can access a .NET array created in the .NET context, like any other .NET object, by using its object reference directly from a .NET class member, or by defining an ABL object reference variable or property and assigning the .NET class member to it.

Using NEW to instantiate a .NET array object in ABL

You can instantiate a .NET array object in ABL by using the NEW function or statement. Here is the syntax:

NEW "type-name-string[]"(dimensions).
type-name-string
A fully qualified type name
dimensions
Represents one or more parameters that define the dimensions of the array

Here is an example:

DEFINE VARIABLE rPointArray AS "System.Drawing.Point[]" NO-UNDO.

rPointArray = NEW "System.Drawing.Point[]"(8).

SetValue( ) instance method

The SetValue( ) instance method stores an object reference at the specified location in the array, overloaded depending on the number of dimensions:

VOID array-object-ref:SetValue( element, index-info )

The array-object-ref is a reference to an array object instance. The element is an ABL primitive value or a reference to an object you want to store as an element of the array. The index-info represents one or more parameters that identify the element location, depending on the array dimensions.

Note: The element parameter is defined as System.Object. So, if element is an ABL primitive type, and you want to store a .NET mapped data type other than the default match for that ABL data type, you must specify the AS option with the AS data type that matches the explicit .NET mapped type (see Pass ABL data types to .NET constructor and method parameters).

GetValue( ) instance method

The GetValue( ) instance method returns an object reference to the element type from the specified location in the array, overloaded depending on the number of dimensions and extents:

System.Object array-object-ref:GetValue( index-info )

The array-object-ref is a reference to an array object instance. The index-info represents one or more parameters that identify the element location, depending on the defined dimensions of the array and their extents. Typically, you want to cast the return value to the type of object that the array stores.

Item default indexed property

The Item default indexed property, defined as System.Object, provides indexed read and write access to each element of an array object, similar to how you use a subscript to access an ABL array. Item is actually an explicit interface member of the System.Collections.IList interface, which System.Array implements.You can read or write all of the array elements using the default property indexer directly on the IList object reference, as in the following example:

DEFINE VARIABLE strList AS "System.Collections.Ilist".
strList = NEW "System.String[]"(3).

// The following 2 lines are setting and getting the indexed property, with an index of 1
strList[1] = "Spinach".  
MESSAGE strList[1] VIEW-AS ALERT-BOX.

The following example is similar, but defines the variable as System.String[], which is not an ABL array or an indexed property. You use SetValue() and GetValue() to set and retrieve the values instead:

DEFINE VARIABLE strArr AS "System.String[]".
strArr = NEW "System.String[]"(3).

strArr[1] = "Spinach".  // Gives an error
MESSAGE strArr[1] VIEW-AS ALERT-BOX.  // Gives an error
 
strArr:SetValue(“Spinach”, 1).
MESSAGE strArr:GetValue(1) VIEW-AS ALERT-BOX.

For more information on default indexed properties, see Access .NET indexed properties and collections, and for more information on explicit interface members, see Access members of .NET interfaces.

For more .NET information on these methods and other members of System.Array, see the .NET Class Library documentation. For an on-line reference, see OpenEdge-installed .NET Controls.

For a working example of accessing a .NET array using System.Array mechanisms, see Example: Access a .NET array.

Note: When you return an ABL object reference from a specified .NET array element, the relationship of that object reference to the object stored in the array element depends on whether the element stores a value type object or a reference type object. If it is a value type object, the ABL object reference points to a separate copy of the object stored by the array element. If it is a reference type object, the ABL object reference points to the same copy of the object stored by the array element. Thus, you must manage and update members of the object stored by the array element differently, depending on the object type. For more information, see Support for .NET object types.