Instantiate and obtain instances of .NET classes
- Last Updated: April 10, 2024
- 3 minute read
- OpenEdge
- Version 12.8
- Documentation
For most .NET class types, you can instantiate the class exactly as you do an ABL user-defined class, by defining a variable or parameter as the given class type and using the NEW function (classes) to invoke a constructor of the class. For example:
|
As when creating any ABL user-defined class
instance, ABL creates this .NET SortedList object
using its default constructor and adds the instance to the session
object chain, because in ABL, all ABL object instances, including .NET ones,
are also instances of Progress.Lang.Object (see
Incorporation of the .NET object model).
You can also obtain an instance of a class created by .NET and assign its object reference to an ABL data item, which also adds the instance to the ABL session object chain. This occurs in the following cases:
- When you access a .NET property or data member that returns a reference to an object created by .NET and assign this object reference to an ABL data item. For more information on accessing .NET properties and data members, see Access .NET class members.
- When you pass an ABL data item as an argument to a .NET output method parameter that returns (and "assigns" to the data item) the object reference of an object created by .NET. For more information on passing .NET method parameters, see Specify .NET constructor and method parameters.
- When you access the event parameters from within an ABL handler for .NET events,
which always passes an input
System.EventArgsobject reference that is created by .NET. There is also an inputSystem.Objectparameter, but you usually already have an object reference to this object, which doesn't need to be added to the session object chain. For more information on using ABL methods and procedures as .NET event handlers, see Handle .NET events.
For example, the Location property
in this code fragment returns a reference to a .NET System.Drawing.Point object
created by .NET for a button control and assigns this object
reference to an ABL variable (rLocation) that is
defined to hold that object reference:
|
However, you can create or obtain a .NET object whose reference does not appear on the ABL session object chain. This happens when the object reference for the .NET object that you create or access is:
- Assigned directly to another .NET property or field (data member)
- Passed directly to an
INPUTparameter of a .NET method - Used directly in an ABL expression (as in a MESSAGE statement) or otherwise never stored in an ABL data element
OUTPUT or INPUT-OUTPUT parameter
to a .NET method, because ABL syntax does not allow the required
colon (:) notation for passing parameters in these
access modes. So, you cannot set a .NET property or data member
object reference value in this way.For example, this code
fragment instantiates a .NET System.Drawing.Size object
and assigns its object reference directly to the Size property
of a .NET button object:
|
In other words, the System.Drawing.Size object
created by the code fragment is never stored as an ABL object reference. In
all cases where you create or return existing .NET objects
from .NET that you assign or pass directly back to .NET, .NET handles
all garbage collection for such objects. (see .NET class instances and garbage collection).
Note
that when you invoke a .NET class constructor or method that
takes parameters defined as a .NET mapped data type, ABL requires
that you pass arguments defined as the corresponding ABL primitive
types. In other words, you cannot define and pass a .NET System.Int32 to parameter
defined as a System.Int32, but must pass an ABL INTEGER instead.
Otherwise, if the parameter is defined as an unmapped .NET object
type (such as System.Drawing.Size), you can pass
an instance of that object type directly to the parameter.
For
example, for the SortedList constructor that takes
a .NET System.Int32 parameter, you can instantiate
the .NET class by passing an ABL INTEGER value:
|
For more information on passing .NET parameters, see the information on accessing .NET methods in Access .NET class members. For more information on working with .NET data types in ABL, see Use .NET data types in ABL.
To create one supported set
of class types—array objects—you must use a completely different
mechanism for creating class instances for them. All .NET array
objects derive from the .NET base class, System.Array.
To instantiate and perform basic operations on array objects, you
must use members of the System.Array base class.
For more information on the instantiation and management of array
objects, see Access and use .NET arrays.