Try OpenEdge Now
skip to main content
GUI for .NET Programming
Using .NET data types in ABL : Accessing and using .NET arrays : Example: Mapping an ABL array to a .NET array
 

Example: Mapping an ABL array to a .NET array

The following example shows how you might access a .NET one-dimensional array using an ABL array:
DEFINE VARIABLE rStrFormat AS CLASS System.Drawing.StringFormat NO-UNDO.

/* Define and initialize ABL array to pass to .NET */
DEFINE VARIABLE dTabs AS DECIMAL EXTENT 4
  INITIAL [20.0, 15.0, 10.0, 5.0] NO-UNDO.

rStrFormat = NEW System.Drawing.StringFormat( ).

/* Pass an ABL array to a .NET System.Single array parameter */
rStrFormat:SetTabStops(0.0, dTabs).
The .NET SetTabStops( ) method takes a one-dimensional System.Single array as its second INPUT parameter. The example passes an ABL four-element array of DECIMAL values as required by the implicit mapping to System.Single.
The following example expands on the ABL array example in the previous section by invoking the StringFormat:GetTabStops( ) instance method to return the tab stops that were previously set using the SetTabStops( ) method. In this case, you do not need to explicitly create the .NET array object, because the GetTabStops( ) method returns one, as shown:
DEFINE VARIABLE rStrFormat AS CLASS System.Drawing.StringFormat NO-UNDO.

/* Define and initialize ABL array to pass to .NET */
DEFINE VARIABLE dTabs AS DECIMAL EXTENT 4
  INITIAL [ 20.0, 15.0, 10.0, 5.0 ] NO-UNDO.

/* Define reference to a .NET System.Single array returned from .NET */
DEFINE VARIABLE rTabsOut AS CLASS "System.Single[]" NO-UNDO.

/* Define ABL array to hold values returned in .NET array */
DEFINE VARIABLE dTabsOut AS DECIMAL EXTENT 4        NO-UNDO.

DEFINE VARIABLE dTabOffset AS DECIMAL NO-UNDO. /* Unused OUTPUT parameter */
DEFINE VARIABLE idx        AS INTEGER NO-UNDO.

rStrFormat = NEW System.Drawing.StringFormat( ).

/* Pass an ABL array to a .NET System.Single array parameter */
rStrFormat:SetTabStops( 0.0, dTabs ).

/* Return a .NET System.Single array
rTabsOut = rStrFormat:GetTabStops( OUTPUT dTabOffset ).

DO idx = 1 TO 4:
  dTabsOut[idx] = UNBOX(rTabsOut:GetValue( idx - 1 )).

  MESSAGE "Tab stop" idx "=" dTabsOut[idx] VIEW-AS ALERT-BOX INFORMATION.
END.
The example defines rTabsOut as an array object of type "System.Single[]" corresponding to the primitive data type array (C# float[]) that the GetTabStops( ) method is defined to return. Because the array is returned as the method's return type, ABL does not automatically map it to an ABL DECIMAL array, but to the specified array object. The code must therefore access the array elements as objects also, and must individually convert them to the ABL DECIMAL data type in order to work with the element values. The ABL built-in UNBOX function (see .NET boxingsupport) allows the example to extract the underlying mapped data type from the .NET System.Object returned by the GetValue( ) method for each array element. Note also the use of 1-based indexing for dTabsOut (the ABL array) and 0-based indexing for rTabsOut (the .NET array).