Try OpenEdge Now
skip to main content
ABL Reference
ABL Syntax Reference : UNBOX function
 

UNBOX function

(Windows only; GUI for .NET only)
Unboxes a .NET System.Object or array object and returns a value of a corresponding ABL primitive or array type.

Syntax

UNBOX ( object-reference )
object-reference
Specifies an object reference to a boxed .NET primitive value (System.Object) or to a one-dimensional .NET array object. For a list of the .NET primitive types that a System.Object can box, see Table 24.
If object-reference points to a boxed .NET primitive value, the function returns a value of the ABL primitive type that implicitly maps to the boxed .NET type.
If object-reference points to a one-dimensional .NET array object, the function returns a copy of the elements contained by the .NET array as an ABL array. If these elements are .NET mapped object types, the returned ABL array contains elements of the corresponding ABL primitive type. Otherwise, the ABL array contains object reference elements of the actual object type contained by the input .NET array.

Example

The following code creates a .NET DataTable with a single DataRow containing two columns, an integer and a character string, and adds data to the two columns in that row. It then processes the data from the "CustNum" (System.Int32) column to get an indication if its value is even or odd and assigns the result to the ABL INTEGER variable iVal. The Item indexed property that is used to access the data has the data type System.Object. So, to use its value in an expression, you must use the UNBOX function to unbox the underlying .NET mapped data type of the System.Object value. In this case, the referenced System.Object represents a System.Int32 value:
USING System.Data.* FROM ASSEMBLY.

DEFINE VARIABLE dataTable1 AS DataTable.
DEFINE VARIABLE dcCustNum  AS DataColumn.
DEFINE VARIABLE dcName     AS DataColumn.
DEFINE VARIABLE row1       AS DataRow.
DEFINE VARIABLE iVal       AS INTEGER.

dataTable1 = NEW DataTable( INPUT "Customer" ).

/* Create columns for a dataTable */
dcCustNum = NEW DataColumn( INPUT "CustNum" ).
dcName    = NEW DataColumn( INPUT "Name" ).
dataTable1:COLUMNS:ADD( INPUT dcCustNum ).
dataTable1:COLUMNS:ADD( INPUT dcName ).

/* Create a new row */
row1 = dataTable1:NewRow( ).

/* Add data to row. */
row1:Item["CustNum"] = 5.
row1:Item["Name"]    = "Mr Jones".

/* Process a value from the row. Without UNBOX, this does not compile.*/
iVal = UNBOX( row1:Item["CustNum"] ) MODULO 2. /* 1 = an odd value */

Notes

*You must use the UNBOX function if you want to reference an appropriate System.Object property or method return value in an ABL primitive expression, such as when performing arithmetic operations together with compatible ABL variables, fields, or literal values. Invoke the UNBOX function directly in the expression, passing it the System.Object as input, where you would otherwise reference the System.Object itself.
*For any direct assignment of a .NET object or object array to a compatible ABL primitive value or array, use of the UNBOX function is optional, because ABL automatically unboxes the underlying .NET object or array object type to its matching ABL primitive or array type.
*If you have a variable or field defined as a compatible ABL array type that you provide as an argument to an OUTPUT parameter of a .NET method defined as a .NET array object, ABL automatically unboxes the .NET array object into the ABL array argument. This automatic unboxing does not occur for an ABL method, procedure, or user-defined function passing the same parameters. In this case, you can use a direct assignment from a compatible .NET array object argument or use the UNBOX function to explicitly do the necessary conversion.

See also

BOX function, Data types