Example: Access a .NET array
- Last Updated: April 17, 2023
- 4 minute read
- OpenEdge
- Version 12.2
- Documentation
This topic first introduces the use of .NET array objects in ABL with the
EventHandlers.p example described in Access .NET objects to demonstrate .NET event handling (see Event handling example).
The following procedure, PointArray.p, is a simpler version of the same
application, which handles fewer .NET events, in order to focus on array handling, in this
case, using an array of System.Drawing.Point objects used to draw a regular
octagon in a graphic region, as shown in the following figure.

From a calculation based on the dimensions of the current client area in a
.NET form, the Point objects of the array specify vertices
between the sides of the octagon (determined by an AdjustOctagon( ) user-defined function). This procedure then calculates and
re-displays the octagon from the point array each time you click the Draw button.
The procedure begins by defining a System.Array object as a point array, a
single Point object used to set elements of this array, and the basic user
interface elements to display the octagon. In particular, the bold code shows the definition
and instantiation of the point array object with the eight points required to draw an
octagon. This code instantiates a new Point object and instantiates the
array object by calling System.Array:CreateInstance( ) with the element
type parameter value obtained using the Point object instance just created.
Note that System.Drawing.Point is a value type (structure). So, .NET
instantiates the array with default (0-valued) Point objects stored in each
element.
|
System.Array reference to a
System.Drawing.Point array type
("System.Drawing.Point[]"), because it later passes the array to a .NET
method that requires this particular array type as a parameter. Otherwise, the procedure
could continue to use the array directly as a System.Array object.Infragistics.Win.Misc.UltraButton class is one of the
OpenEdge Ultra Controls for .NET provided with OpenEdge, and
Progress.Windows.Form is an OpenEdge extension of
System.Windows.Forms.Form designed specifically for running in the ABL
environment. For more information on these objects, see Create and using forms and controls.Also note that if this procedure did not create an instance of the array element type
(System.Drawing.Point) to set its array element values, it could obtain
the array element type information needed to instantiate the array object using the static
GetType( ) method of the Progress.Util.TypeHelper class. The code to support this method
could look like the following code in bold:
|
PointArray.p continues with the following code to initialize the user
interface elements and graphical constants used to build and display octagons. To create a
.NET form, the procedure uses the OpenEdge .NET class, Progress.Windows.Form, which, as
previously noted, is derived from the System.Windows.Forms.Form class.
|
To actually draw the octagon, the example subscribes an internal procedure,
Draw, as a handler for the Click event on the form
button. Thus, when you click the button referenced by rDrawBtn, the
procedure updates the octagon point coordinates and draws it in the form. For more
information on handling .NET events, see Handle .NET events.
The Draw event handler procedure that follows responds to the button
Click event by drawing the octagon from the point array returned by the
AdjustOctagon user-defined function. It is the
DrawPolygon( ) method that requires that the array object be passed as a
"System.Drawing.Point[]" object rather than as the
System.Array super class object initially created.
|
PointArray.p procedure also demonstrates the technique
for drawing graphics using the graphics object (rGraphics) that is
associated with a .NET form. You can only draw form graphics in a form event handler,
because the associated graphics object is available for drawing only during the handling of
a form event.As noted previously, the AdjustOctagon user-defined function determines
all the points of the array required to draw an octagon within the current client area of
the form. This, then, is where ABL uses the single System.Drawing.Point
object (rPoint) to set each element of the array using a 0-based index
(idx).
|
ABL can use this single System.Drawing.Point object to set each array
element, because, as a value type, .NET maintains its own copies of all the
Point objects stored in the array. ABL can then reset the members of all
the array element objects from a single modified object that ABL passes to the
SetValue( ) method. After all the Point objects have
been set, the function returns the updated point array as a value for use in the
Draw event handler (previously described).
The following CalcOctagonSide internal procedure uses a formula based on
the square root of 2 to calculate the horizontal or vertical components used to derive
coordinates for each point of the octagon.
|