Generic Using Phrase

Generic types and methods can be defined in COBOL with the USING phrase.

generic-using-phrase

USING generic-argument generic-argument

generic-argument

generic-argument conventionally has a single character upper case name such as T and U.

Example

In this example, the main method finds and displays the greater and the smaller number from two ones. T is a generic argument to the Min method, which in this case is a static generic method. The Min method expects two parameters both of which should be of the same type as the generic parameter. The CompareTo method may be invoked since T is known to implement the IComparable interface.

       class-id GenericMath.

       method-id main static.
       01 n1 binary-double.
       01 n2 binary-double.
           set n1 to 999
           set n2 to 100
           display n1 space n2
           display Min[binary-double](n1 n2)
           display Max[binary-double](n1 n2)
           goback.
       end method.
       method-id Min using T static.
       constraints.
           constrain T implements type System.IComparable.
       procedure division using by value item1 as T item2 as T
                      returning res as T.
           if item1::CompareTo(item2) < 0
               set res to item1
           else
               set res to item2
           end-if
       end method.
       method-id Max using T static.
       constraints.
           constrain T implements type System.IComparable.
       procedure division using by value item1 as T item2 as T
                      returning res as T.
           if item1::CompareTo(item2) < 0
               set res to item2
           else
               set res to item1
           end-if
       end method.

       end class.

See also the Generics samples, which are available from Start > All Programs > Micro Focus Enterprise Developer > Samples > Visual COBOL Samples, under COBOL for .NET.