Previous Topic Next topic Print topic


COBCH1564 First parameter to an extension method must be passed BY VALUE

The first parameter passed to an extension method must be a value parameter, not reference parameter.

To define an extension method:

  • Use the EXTENSION keyword in the method header.
  • The method must be in a static class that is not nested or generic. The method is implicitly static.
  • In the method body, the first parameter is the type to extend. It is always a value parameter.

Example:

The following extension method extends the string class, by adding a method to count the number of words in a string:

       class-id MyCount static.
       method-id CountWords extension. *> extension method is implicitly static
       procedure division using by value str as string 
                          returning wordCount as binary-long.
            set wordCount to str::Split(' ')::Length
       end method.
       end class.
Previous Topic Next topic Print topic