Access .NET arrays
- Last Updated: April 17, 2023
- 5 minute read
- OpenEdge
- Version 12.2
- Documentation
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.
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:
|
- 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:
|
Using the CreateInstance( ) method to instantiate a .NET array object in ABL
You can also instantiate a .NET array object in ABL, by using the CreateInstance() method. The rest of this section describes how
to do that. Note that this is not the preferred method. It is simpler and easier to use the
NEW statement or function.
Recall that the base .NET class for all array objects is System.Array. This class also provides the mechanism you must use
to create and manage instances of all .NET array classes. In other words, System.Array is both the base class and the factory for creating
and managing .NET arrays of all types. When you use System.Array to create a .NET array instance, it creates an instance of the
specified .NET array type, even if you assign the object reference that you create to a data
element of type System.Array. You can continue to use the
System.Array data element to access elements of the
created array type. You can also cast the System.Array
instance as necessary to make it type compatible with a method parameter or property
assignment.
Thus, the System.Array class provides a
variety of public static and instance members that you can use to create and manage .NET
array objects. The basic System.Array members for working
with array objects include the:
-
CreateInstance( )static (factory) method — Creates and returns an instance ofSystem.Arrayfor a given element type. This method is overloaded to create arrays from one to any number of dimensions. The first parameter of this method is aSystem.Typethat describes the object type of each element in the array contents. You can provide this element type as the value returned by one of the followingGetType( )methods:- OpenEdge static Progress.Util.TypeHelper:GetType( ) method
- Microsoft .NET static
System.Type:GetType( )method, as long as the type resides in the .NET core assembly (mscorlib.dll) - .NET
GetType( )method on an instance of the class that the array contains
All three
GetType( )methods return an appropriateSystem.Typeobject that corresponds to a given object type name specified as a string:
|
The class-type-or-instance can be one of the following, depending on how you obtain the element type:
- Progress.Util.TypeHelper
-
System.Type - An object reference to an instance of the class that can be stored as an element of the array
Note that you specify the type name (type-name-string) using a fully qualified type name and only when using the
GetType( ) static method on Progress.Util.TypeHelper or System.Type. The
instance GetType( ) method takes no parameters. The
dimensions represents one or more parameters that
define the dimensions of the array.
GetType( )
method on Progress.Util.TypeHelper in order to reference a
type that does not reside in the .NET core assembly, you must include the assembly where the
type resides in your working assembly references file (assemblies.xml). For more information on assembly references files, see Identify .NET assemblies to ABL. When CreateInstance( ) creates the
specified array, it initializes the array elements, depending on the type. For reference
types, it initializes each element to a null reference. For
value types, it initializes each element to its default or 0 value.
-
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 asSystem.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 theASoption with theASdata type that matches the explicit .NET mapped type (see Pass ABL data types to .NET constructor and method parameters). -
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.
-
Itemdefault indexed property — This property, defined asSystem.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.Itemis actually an explicit interface member of theSystem.Collections.IListinterface, whichSystem.Arrayimplements. If you cast an array object to this interface type, you can then read or write all of the array elements using the default property indexer directly on theIListobject reference, as in the following example:USING Progress.Util.* FROM ASSEMBLY. DEFINE VARIABLE strArr AS CLASS System.Collections.IList NO-UNDO. strArr = CAST(System.Array:CreateInstance (TypeHelper:GetType("System.String"), 2), System.Collections.IList) strArr[1] = "Spinach". MESSAGE strArr[1] VIEW-AS ALERT-BOX.This code fragment defines
strArras an object reference to theIListinterface, and it casts a new string array object reference to this interface before assigning that object reference tostrArr. It then assigns a string,"Spinach", to the second element of the array (strArr[1], again, zero-based) and reads the same element to display the string in a message. 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 Class Library documentation of the
Microsoft .NET Framework SDK. 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.