Invoking a Method

You can invoke a method in two ways: by using the invoke statement or by using the inline invocation syntax.

INVOKE statement

For example, the following INVOKE statement invokes the replace method on the input string obj-string and returns the results in obj-string. Notice that the case of the method name is different for .NET and JVM. It is replace in .NET and Replace in JVM.

       invoke obj-string::replace("Some" "New")   *> .NET
       invoke obj-string::Replace("Some" "New")   *> JVM 
       display obj-string

Inline invocation

Similarly, the following SET statement uses the inline invocation syntax, which employs an implicit set property method. This example statement invokes the Replace() method on the input string obj-string and sets the results in the aString variable.

       set aString to obj-string::replace("Some" "New")   *> .NET
       set aString to obj-string::Replace("Some" "New")   *> JVM
       display aString 

In addition, you can use the inline invocation syntax to access properties. In the following example, the Length property of the string myString is accessed and is displayed:

       display "Total characters is " myString::Length

You can invoke any method in the same compilation as the invoking code, either a static or instance method, as follows:

Invoking a static method

To invoke a static method, you specify the class name, followed by :: and then the method name. For example, SimpleClass::StaticMethod as in the invoke statement below:

       invoke type SimpleClass::StaticMethod
       ...
       class-id SimpleClass.
       method-id StaticMethod  static.
          display "In static method"
       end method.
       end class.

Invoking an instance method

To invoke an instance method in the same compilation unit, you instantiate an object first. You can then specify the instance object, followed by :: and then the method name. For example, mySimpleObj::InstanceMethod as in the invoke statement below:

       01 mySimpleObj type SimpleClass.
       set mySimpleObj to new SimpleClass("Hello")
       invoke mySimpleObj::InstanceMethod
       ...
       class-id SimpleClass.
       01 thisString string.

       method-id New.
       procedure division using by value aString as string.
           set thisString to aString
       end method.

       method-id InstanceMethod.
       procedure division.
           display "In instance method"
           display "Instance data is " thisString
       end method.

Examples

See also the Core sample, which is available from Start > All Programs > Micro Focus Visual COBOL > Samples , under COBOL for JVM (Windows) or $COBDIR/demo (UNIX).