Implement .NET interfaces in ABL
- Last Updated: June 19, 2019
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
As with overriding methods of an inherited .NET class (see Override .NET methods), when you implement a method of a .NET interface, you must define the method in ABL exactly as specified by the .NET method prototype. This means that the name, return type, and the signature of the implementing ABL method must match the .NET method prototype with respect to the number of parameters, the corresponding parameter modes, and the corresponding data types. Define the method parameters exactly as you do for the return values and parameters of ABL-overridden .NET methods (see Override .NET methods). See Table 1 for a summary of the same rules for defining data types and parameter modes for ABL-overridden .NET methods.
When you implement
a property of a .NET interface, you must define the ABL property
with an implementation that is compatible with the specified .NET property
prototype. So, your ABL property must define the same property name,
the same .NET data type, and a compatible pattern of GET and SET accessors.
If the .NET property prototype has a C# get,
the implementing ABL property must have a GET accessor,
and if the .NET property prototype has a C# set,
the implementing ABL property must have a SET accessor.
However, if the .NET property prototype specifies only one
accessor, the implementing ABL property can add an implementation
for the missing one. If the data type of the interface property
prototype is a .NET mapped data type, you must define the
ABL property data type using the rules for explicitly mapping .NET data
types. For more information, see Explicit data type mappings.
When
you implement an event of a .NET interface, you must define
the event exactly as specified by the .NET event prototype,
defining the same event name and signature. To define the same signature
as a .NET event prototype, you must use the DELEGATE option
of the ABL DEFINE EVENT statement to
specify the same .NET delegate type that is used to define
the event prototype. For more information on .NET delegate
types, see Handle .NET events.
As with ABL, all .NET interface members
are PUBLIC.
For example, if you wanted to
implement the .NET System.Collections.ICollection interface, this
is the C# declaration for it:
|
This is the C# declaration for the inherited System.Collections.IEnumerable interface:
|
Finally, this is the C# declaration for the System.Collections.IEnumerator interface,
the object type returned by the GetEnumerator( ) method:
|
The following ABL CustNameCollection class then
implements the ICollection and IEnumerable interfaces:
|
In this case, while all the properties and methods
of these interfaces are implemented, only the Count property
of the ICollection interface and the GetEnumerator( ) method
of the IEnumerable interface are implemented with
any functional behavior. The remaining members of CustNameCollection throw
the .NET System.NotImplementedException object
when accessed, because this is the standard exception to throw in .NET when
you do not implement functionality for a member of an interface
or in an overridden method.
The GetEnumerator( ) method returns an
IEnumerator object implemented by the ABL CustNameEnumerator class, which creates an ABL query on the
Customer table of the Sports2020 database. The
constructor returns the number of records in the opened query as an OUTPUT parameter and the GetEnumerator( )
method stores the result in the Count property.
The following ABL CustNameEnumerator class implements
the IEnumerator interface:
|
This class implements all the members of the IEnumerator interface,
which allow you to get the value of the Name field
returned by a private query data member from a given record of the Customer table.
The Current property returns the current Name value
or the Unknown value (?) if the query has just been opened,
is at the end, or has been reset to the beginning by the Reset( ) method,
and the MoveNext( ) method gets the next
record in the query. Note that the class constructor uses the NUM-RESULTS attribute
on the query handle to return the value for the Count property
of ICollection. Thus, all the implemented .NET properties
and methods function and interpret ABL data in a manner that is
understandable in the .NET context.