Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

1.6 COBOL Classes and Language Interoperability

In some cases we may have several methods (similar to functions) that do different things to the same data, say a record. COBOL collects these methods in what is called a class. A class describes the data, along with the methods that operate on the data. An object is really just an instance of a class. We can have one or more instances of a given class. You would code a class as you would any COBOL program, with the four divisions, that is, Identification, Environment, Data, and Procedure. Appendix A.4 shows the general rules of the Fujitsu COBOL language. Appendix A.5 shows the definition of the program structure for a Fujitsu object COBOL program. Let's look at a COBOL class listing:

000010 CLASS_ID. BIGDOG AS "BigDog" INHERITS DOG.
000020 ENVIRONMENT DIVISION.
000030 CONFIGURATION SECTION.
000040 REPOSITORY.
000050    CLASS DOG AS "Dog".
000060 OBJECT.
000070 PROCEDURE DIVISION.
000080 METHOD-ID. BARK AS "Bark" OVERRIDE
000090 DATA DIVISION.
000100 WORKING-STORAGE SECTION.
000110 77 I PIC S9(9) COMP-5.
000120 LINKAGE SECTION.
000130 77 Bark-Count PIC S9(9) COMP-5.
000140 PROCEDURE DIVISION USING BY VALUE Bark-Count.
000150    -PERFORM VARYING  I  FROM 1 BY 1 UNTIL I > Bark-Count
000160       DISPLAY "WOOF WOOF (COBOL)"
000170    END-PERFORM.
000180 END METHOD BARK.
000190 END OBJECT.
000200 END CLASS BIGDOG.

This program (bigdog.cob) and many other examples are on the companion FTP site for this book, http://www.phptr.com/reeves. Notice that this class BIGDOG inherits from another class called DOG. It turns out that the class DOG is a Visual Basic or VB.NET class. This program is an example of one of the outstanding new features of the .NET Framework, that is, any .NET-enabled language can inherit from any other .NET-enabled language. Let's look at the VB class listing, dog.vb, for the Dog class.

Imports System

Public Class Dog
    Public Sub RollOver()
        Dim R As New Random()
        Console.WriteLine("Scratch my tummy.")
        Bark(r, Next(1, 4))
    End Sub

    Public Overridable Sub Bark(ByVal times As Integer)
        Dim I As Integer
        For I = 1 to times
            Console.WriteLine("WOOF WOOF (VB)")
        NEXT I
    End Sub
End Class

To further illustrate this interoperability between .NET-enabled language, let's look at the C# (pronounced "C Sharp") driver class for this example.

using System;

public class Demo
    public static void Main()
        {
            Dog d = new Dog();
            BigDog b = new BigDog();

            d.RollOver();
            b.RollOver();

            Console.WriteLine("Sleep");
        }

Notice how this class is instantiating a Visual Basic object d and then instantiating a COBOL object b. It then calls the RollOver method from both the Visual Basic and the COBOL classes.

With a very simple example we have formed a relationship between three different .NET-enabled languages. By working with the three different classes and then the instantiated objects we have been able to communicate between the languages and perform some programming work. All three languages are handled in the same fashion using the Visual Studio.NET IDE. Now the powerful tools for Windows development is available to the COBOL developer just as in the past they were for C++ and Visual Basic (VB). By the way, the C# class could just as well have been a Visual C++ class. (This project is on the FTP site under the folder MultiDog.) What we would do then is bring up the Visual Studio.NET. Under the File pull-down, we would open the solution to the folder MultiDog. We would then open the project icon for the dog project. At this point we would see a screen, as shown in Figure 1–5. Under the Build pull-down we would click on Build

Figure 1-5Figure 1–5 Dog Project Code

Solution. Once it shows a good build we would use the Debug heading and the pull-down Step Into. This would show the screen in Figure 1–6. Using the F10 function key, we would step down to the line just after the Console.WriteLine. Then we would click on the icon on the status bar for the console screen. This would show the Console screen and the results of running the program. See how we have outputs from all three objects for each language—VB, C#, and COBOL? At this point under the Debug pull-down we would stop the debugger. Then we would go over to File pull-down and close the solution. I have thrown a lot at you at this point, but it certainly shows how easy it is to work with classes from different .NET-enabled languages and perform work.

Figure 1-5Figure 1–6 Execution Results of Dog Code

  • + Share This
  • 🔖 Save To Your Account