.NetCobol invoking a C# method. Error COBCH0829.

.Net Cobol class:

      $set ilusing "CSharpPrograms"

       class-id. CobolMain.

       method-id. main public.

           invoke type Console::WriteLine("CobolMain: Hello World")

           invoke type CSharpSub::DisplayMethod()

       end method main.

       end class.

  

C# Class:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

//  This is an experiment of calling a C# program from a Cobol main program.

 

namespace CSharpPrograms

{

  public class CSharpSub

  {

    public void DisplayMethod()

    {

      Console.WriteLine("CSharpSub: Hello World");

    }

    static void Main()

    {

    }

  }

}

  

When building, I get the error below in the Cobol class.  I have tried many things.  Is there some special Cobol syntax I am not using?  Thanks.

 

Error 1     COBCH0829: Could not find method 'DisplayMethod' with this signature

  • There are a couple of problems with your example.

    1. You have a main method in both the COBOL project and the C# project. You should remove the main method from the C# project and make the main method in COBOL a static method.

    2, The invoke in the COBOL program is using the type specifyer which is used to invoke static methods yet the C# method is not defined as being static. If you wish to call an instance method in the C# program then you must first create an instance of the class in COBOL and then call the method on the instance:

    Example:

    01 myinstance  type CSharpPrograms.CSharpSub.

      set myinstance to new CSharpSub

      invoke myinstance::DisplayMethod

    To call it as a static method you would use the following:

         $set ilusing"CSharpPrograms"
          class-id. CobolMain.
          method-id. main static.
          procedure division.
              invoke type Console::WriteLine("CobolMain: Hello World")
              invoke type CSharpPrograms.CSharpSub::DisplayMethod
          end method main.
          end class.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace CSharpPrograms
    {
       public class CSharpSub
       {
           public static void DisplayMethod()
           {
               Console.WriteLine("CSharpSub: Hello World");
           }
       }
    }