Skip to main content

This article explains how you can write a COBOL .NET class that inherits from a C# .NET class.

Problem:

You need to create a COBOL .NET class that inherits from a C# .NET class.

Resolution:

You create a COBOL .NET class that inherits from a C# .NET class in the same way as you do it for a VB .NET class. The .zip file attached to this article includes a MS Visual Studio 2008 solution, CsharpAndCOBOLinherit.sln, that demonstrates how you can do that.

The solution consists of the following 3 projects:


  • ConsoleApplicationCsharp - a C# Console Application project
  • ClassLibrarycSharp - a C# Class Library project
  • ClassLibrary1Cobol - a COBOL Class Library project

The C# Console Application, ConsoleApplicationCsharp, invokes the COBOL Class Library, ClassLibrary1Cobol, which inherits from the C# Class Library, ClassLibrarycSharp.

Open the COBOL program, Class1.cbl, in the code editor and see that it defines the COBOL class, Class1. Class1:


  • inherits from a C# class
  • sets in its constructor the str variable of its superclass Class1Csharp
  • sets in an instance method the str variable of its superclass Class1Csharp

This is the contents of Class1.cbl file:


class-id. Class1 as "ClassLibrary1Cobol.Class1" inherits "ClassLibrarycSharp.Class1Csharp".
environment division.
configuration section.
repository.

static.
working-storage section.

end static.

object.
working-storage section.

method-id. NEW.
procedure division.
display "ClassLibraryCOBOL.Class1 Constructor()"
goback.
end method NEW.

method-id. NEW.
procedure division using aStr as String.
display "ClassLibraryCOBOL.Class1 Constructor(String)"
set super::"str" to aStr *> sets the str variable of the superclass Class1Csharp in a Constructor
goback.
end method NEW.

method-id. "InstanceMethod".
local-storage section.
procedure division.
set super::"str" to "456" *> sets the str variable of the superclass Class1Csharp in an Instance method
goback.
end method "InstanceMethod".

end object.
end class Class1.

Open Class1Csharp.cs from the ClassLibrarycSharp project in code view. The following code defines the C# class Class1Csharp that the COBOL class inherits from:


...
namespace ClassLibrarycSharp
{
public class Class1Csharp
{
public String str = null;
}
}

Program.cs from the C# Console Application, ConsoleApplicationCsharp shows how you can use the COBOL .NET class inheriting from the C# .NET class:


...
namespace ConsoleApplicationCsharp
{
class Program
{

static void Main(string[] args)
{
ClassLibrarycSharp.Class1Csharp cla1 = new ClassLibrarycSharp.Class1Csharp();
cla1.str = "123";

ClassLibrary1Cobol.Class1 cla1Cobol1 = new ClassLibrary1Cobol.Class1(); // Creates an instance of the COBOL Class Library Class1.
cla1Cobol1.str = "123"; // Sets the "str" variable which belongs to the C# Class1Csharp
// and which is a superclass of the COBOL Class Library Class1
// since the COBOL Class Library Class1 inherits from the C# Class1Csharp.

ClassLibrary1Cobol.Class1 cla1Cobol2 = new ClassLibrary1Cobol.Class1(ref cla1.str);
// Creates an instance of the COBOL Class Library Class1
// passing a string to the Constructor of the COBOL class.
// The COBOL constructor sets the "str" variable
// that belongs to the C# Class1Csharp.

cla1Cobol2.InstanceMethod(); // The method InstanceMethod of the COBOL class
// sets the "str" variable that belongs to the C# Class1Csharp.

cla1.str = cla1Cobol2.str; // Gets the "str" variable which belongs to the C# Class1Csharp
// and which is the superclass of the COBOL Class Library Class1.
}
}
}


CsharpAndCOBOLinherit.zip




Date:

Name:

Description of change:
Date:

Name:

Description of change:

Old KB# 14545