COBOL Programming Using .Net Framework
Authors: Ron D. Reeves, Ronald Reeves
Venue:  (2001)

From the Book:PrefaceWindows 2000 is a large and important system, and it is the core of a more embracing architecture that Microsoft calls Windows DNA 2000. In this context, DNA stands for Distributed interNet Applications and represents Microsoft's vision for building distributed systems. This type of architecture is focused on developing the new "digital nervous system" for enterprises. In this context, the "digital nervous system" is the corporate, digital equivalent of the human nervous system: an information system that can provide a well-integrated flow of information at the right time, to the right place in an organization. Such systems can be programmed at many levels, from the lowest level of device drivers giving access to privileged instructions to very high levels using powerful software application development tools. This book is aimed at Microsoft Windows application programming, using the new Fujitsu COBOL compiler and the Visual Studio.Net development environment. The Fujitsu COBOL and Visual Studio.Net discussions and examples are based on the Release Candidate from Microsoft. The book aims to cover most of the important areas needed for practical application development using Fujitsu COBOL, without attempting to be encyclopedic. The book is largely self-contained, including coverage of the core topics of Win32 programming required for Microsoft Windows. The book should prove suitable for programmers migrating to Microsoft Windows from other environments such as Unix and mainframes. A large part of the book addresses issues of which components actually make up the .NET Framework and the Microsoft Windows operating systems. One must realize there are numerousconstraints among all the components, and one needs to try to understand from the beginning how they fit into the whole .NET Framework and the Microsoft Windows operating system.Learning such complex technology can be quite a challenge. The documentation is vast, equivalent to tens of thousands of printed pages, and it is changing all the time. You can subscribe to various Internet discussion groups, and you will receive hundreds of emails every day. There are many, many books on different parts of this technology. But how do you grasp the whole picture? This book aims to be holistic, to provide a practical guide for the COBOL programmer who has used any COBOL compiler. The book provides a tutorial, giving you all the basic information you need to create working .NET Framework application systems. The book and companion FTP site (http://www.phptr.com/reeves) have many example programs in Fujitsu COBOL to aid you in gaining an understanding of how the whole environment fits together.Before we get into Chapter 1, lets take a snapshot of some of the types of areas and examples covered in the book.COBOL on the WebWith an increasing amount of business being conducted on the Web, organizations everywhere are asking themselves how to best take advantage of the enormous new business opportunities. Businesses with a lot of legacy COBOL are even more concerned, since most of their applications were built long before the Web became the business phenomenon that it is today. These businesses find that retraining their workforce to use other programming languages, as well as retooling their applications for the Web is a daunting task. COBOL vendors, including Fujitsu, have been selling a variety of solutions for migrating COBOL applications to the Web. Fujitsu's support for the .NET Framework builds on that position and takes several steps forward, allowing COBOL programmers to program directly to the Web and to easily use their existing legacy code.ASP.NET (language = "COBOL")ASP.NET is the Microsoft successor to Active Server Pages (ASP), which was designed to deliver dynamic Web content. ASP faced a number of weaknesses in the areas of performance and programmability, and ASP.NET makes substantial improvements in those areas. Instead of being interpreted like ASP, ASP.NET compiles pages to native code. This change results in enormous performance benefits, with only a one-time cost to compile the page when it is first deployed. The other important side effect of the design choice (to compile instead of interpret) is that it opens the door for compiled languages to provide code in ASP.NET pages. Fujitsu COBOL is taking advantage of this important benefit by allowing COBOL programmers to embed COBOL code in ASP.NET pages. The following is a sample ASP.NET page written using Fujitsu COBOL that displays "Hello COBOL world!" in increasing font sizes:<%@ page language="COBOL" %><script runat="server"> OBJECT. DATA DIVISION. WORKING-STORAGE SECTION. 77 FONT-SIZE PIC S9(9) COMP-5. END OBJECT.</script><% PERFORM VARYING FONT-SIZE FROM 1 by 1 UNTIL FONT-SIZE > 7 %><font size="<%=FONT-SIZE%>"> Hello COBOL world! </font> <br><% END-PERFORM. %>The "<%@" characters introduce the directive that allows us to set the programming language for the page to COBOL. The <sc block allows us to introduce COBOL code that declares the FONT-SIZE variable and the characters "<%" allow us to introduce inline COBOL code to loop over the HTML that displays "Hello COBOL world!". ASP.NET also makes programming interactive content much simpler by exposing standard HTML elements as controls and allowing programmers to create new controls. Web Forms is the name given to this type of control in the .NET Framework. The sample below shows the use of several Web Forms controls in an application that displays the image of a piece of fruit based on the fruit name selected in a drop-down box.<html><head> <script runat="server" language="COBOL"> ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. PROPERTY SELECT-VALUE AS "Value" PROPERTY IMAGE-SRC AS "Src" PROPERTY SPAN-VALUE AS "InnerHtml" CLASS STRING-BUILDER AS "System.Text. StringBuilder" CLASS SYS-STRING AS "System.String" CLASS SYS-OBJECT AS "System.Object" CLASS EVENTARGS AS "System.EventArgs". OBJECT. METHOD-ID. FRUITLIST-CLICK. DATA DIVISION. WORKING-STORAGE SECTION. 77 IMAGE-FILE-BUILDER OBJECT REFERENCE STRING-BUILDER. 77 IMAGE-FILE-STR OBJECT REFERENCE SYS-STRING. LINKAGE SECTION. 77 EVENT-SOURCE OBJECT REFERENCE SYS-OBJECT. 77 EVENT OBJECT REFERENCE EVENTARGS. PROCEDURE DIVISION USING BY VALUE EVENT-SOURCE EVENT. MOVE SELECT-VALUE OF FRUITLIST TO SPAN-VALUE OF FRUITNAME. INVOKE S IMAGE-FILE-BUILDER. INVOKE IMAGE-FILE-BUILDER "ToString" RETURNING IMAGE-FILE-STR. MOVE IMAGE-FILE-STR TO IMAGE-SRC OF FRUITIMAGE. END METHOD FRUITLIST-CLICK. END OBJECT. </script></head><body><form runat="server"> <font face="Verdana"> <b>Please Select A Fruit : <b> <select id="FRUITLIST" runat="server" size="1"> <option value="Orange">Orange</option> <option value="Apple">Apple</option> <option value="Mango">Mango</option> <option value="Pear">Pear</option> <option value="Banana">Banana</option> </select> <input type="submit" value="submit" runat="server" OnServerClick="FRUITLIST-CLICK"> <p> <table> <tr> <td> <img id="FRUITIMAGE" src="images\black.gif" runat="server" /> </td> <td> <font face="Verdana" size=6> <span id="FRUITNAME" runat="server"/> </font> </td> </tr> </table></form><body></html>The <form> tag block surrounds the set of Web Forms controls used in the page. The runat="server" attribute indicates to ASP.NET that the behavior of the control is implemented using code running on the server. This example has four Web Forms controls, whose names are event-handlerin this case "FRUITLIST-CLICK." An image control called "FRUITIMAGE" that is used to display the image of the selected fruit. A span control called "FRUITNAME" that displays the name of the selected fruit alongside the image.The <script> block at the top of the ASP.NET page implements the "FRUITLIST-CLICK" event handler in COBOL for the input button control. The method extracts the selected name from the "FRUITLIST" control, uses the StringBuilder class from the .NET Framework base classes to construct the path to an image file of the fruit, and sets the appropriate value for the "FRUITIMAGE" and "FRUITNAME" controls. This technology enhances the programmability of Web pages and helps to overcome limitations of static HTML technology. With this technology you can imagine constructing sophisticated Web systems, including applications like electronic storefronts, inventory management systems, shipping management, and much more. Best of all, these applications can be programmed using COBOL if that is what your workforce is trained to use.Web Services: Programming the WebWhile ASP.NET allows programmers to package dynamic contents onto server-side Web pages, Web Services go a step further. They provide the opportunity to expose programming interfaces to the Web for use by clients to package in any way they see fit. This allows businesses to truly componentize their product offerings in ways that end-users can customize to suit their own applications. The following is an example of a very simple Web Service written in COBOL:<%@ webservice language="COBOL" %> CLASS-ID. FOO. FACTORY. PROCEDURE DIVISION. ADDME. DATA DIVISION. LINKAGE SECTION. 01 OPND-1 PIC S9(9) COMP-5. 01 OPND-2 PIC S9(9) COMP-5. 01 RET PIC S9(9) COMP-5. PROCEDURE DIVISION USING BY VALUE OPND-1 OPND-2 RETURNING RET. COMPUTE RET = OPND-1 + OPND-2. END METHOD ADDME. END FACTORY. END CLASS FOO.The example is just a COBOL class definition with a method called "ADDME" that adds two numbers together. The "webservice" directive at the beginning tells the .NET Framework that we want to expose methods as Web Services and identifies the programming language in which the methods are written. (In future versions, users will probably need to use a compiler directive to specify an attribute for each method that is to be exposed as a Web Service.)As simple as this seems, a number of technologies all have to work together for Web Services to function. For example, SOAP (Simple Object Access Protocol) is a protocol that Microsoft would like to see adopted as a standard; it is used for marshalling data using XML (eXtensible Markup Language). SOAP is needed for clients and servers to communicate their arguments and results in a format that can ultimately be transmitted over HTTP. Microsoft has also defined a Service Description Language (SDL), which describes the services being exposed and provides clients the mechanism through which they can find those services. Web services open up new opportunities for application deployment. Instead of packaging business logic applications with wrappers that predetermine the Web presentation style and content, companies can expose their business logic as a Web Service to be customized for use by their customers.Frameworks and Language Inte comes with base classes that implement a wide variety of operations, such as I/O, data type manipulation, and graphical application development. Because Fujitsu COBOL is targeting the .NET Framework, the language in which these base classes are implemented is immaterial. COBOL programs can use these features as if they were written in COBOL. The infrastructures for this language interoperability is a Common Language Runtime (CLR). The previous ASP.NET fruit list example has already demonstrated some of the uses of .NET Framework. The COBOL method "FRUITLIST-CLICK" was embedded in a class that inherited from base classes in the .NET Framework. This allowed direct access to inherited properties. The "StringBuilder" class is another example of a class from the .NET Framework Base Classes. The following sample shows the use of the Win Forms part of .NET Framework that allows developers to construct equivalent Win32 API-based Graphical User Interface (GUI) applications:CLASS-ID HELLO INHERITS FORM.ENVIRONMENT DIVISION.CONFIGURATION SECTION.REPOSITORY. PROPERTY WIN-TEXT AS "Text" CLASS APPLICATION AS "System.WinForms.Application" CLASS FORM AS "System.WinForms.Form".FACTORY.PROCEDURE DIVISION.METHOD-ID. MAIN.DATA DIVISION.WORKING-STORAGE SECTION.77 APP-OBJ USAGE OBJECT REFERENCE FORM.PROCEDURE DIVISION. INVOKE HELLO "NEW" RETURNING APP-OBJ. MOVE "Hello COBOL World!" TO WIN-TEXT OF APP-OBJ. INVOKE APPLICATION "Run" USING BY VALUE APP-OBJ.END METHOD MAIN.END FACTORY.END CLASS HELLO.This very simple application brings up a frame window with the title "Hello COBOL World!". It demonstrates inheritance from the Win Forms "Form" class and use of methods and properties in class.Migrating COBOL Code to the Microsoft .NET FrameworkThe .NET Framework provides interoperability between code targeted for the CLR, also known as managed code, and existing native code and COM applications. This means that developers can write new managed code that makes calls to their existing code. In the case of existing COM applications, this is as simple as using a type library import tool (TLBIMP) that makes COM interfaces visible to managed code. For other native code, users simply need to provide a COBOL prototype declaration for the code they want to call. To call from native code to managed code, developers can use a type library exporter (TLBEXP) and an assembly (mechanism of packaging) registration tool (REGASM) to export their managed code as COM objects. Eventually, users will also have the option of compiling their existing code with the new compiler to run completely using the CLR. While Fujitsu Software is committed to enabling this scenario, early releases of the compiler are likely to not support all of COBOL's features. New COM-type development will probably use COM+, which has superseded COM. However, COM can still be used for component development if desired.Delivering on the VisionAs the only COBOL vendor to currently target the new Microsoft .NET Framework environment, Fujitsu Software has a commitment to a vision of seamless interoperability and powerful programming tools. The price of this commitment is the development of an enhanced COBOL compiler with language extensions to support the new .NET Framework platform, a completely new code generator targeting Microsoft's Intermediate Language (IL or metadata) and ASP.NET support code for COBOL, among other things.Language ExtensionsThe CLR is based heavily on an object-oriented programming model. This does not mean that all programs that run in the environment have to be object-oriented, but object-oriented language constructs are needed to use some features of the environment. The Fujitsu COBOL implementation will conform to the most current COBOL draft standard where possible. Some features of the CLR are not supported by the current state of the COBOL draft standard. For example, constructs like delegates, custom attributes, and visibility attributes have no existing representation in COBOL. For these constructs, Fujitsu will try to ensure that standards conforming programs are not affected by the existence of language extensions targeted to the CLR.Code GenerationThe CLR achieves its goals of interoperability, security, and robustness by operating on metadata instead of native code. This means that compilers that target the environment have to develop new code generators that generate metadata instead of native code. The CLR uses just-in-time (JIT) compilation strategies to ultimately translate code for the platform into fast native code. Enhanced interoperability also means that applications with code written in different programming languages can also be seamlessly debugged. This is an important productivity benefit, particularly since most current development environments for COBOL (with the exception of Fujitsu COBOL) make debugging cross-language applications quite cumbersome.ASP.NETIn addition to a new compiler, language-specific support is also required for the new ASP.NET infrastructure. Because ASP.NET compiles its pages, it has to be able to generate code to represent the HTML and ASP.NET content in the language in which the page is written. For example, the "Hello COBOL world!" ASP.NET sample shown earlier results in generated code that includes the following fragment:PERFORM VARYING FONT-SIZE FROM 1 BY 1 UNTIL FONT-SIZE > 7 INVOKE ASP_output "Write" USING BY VALUE " <font size=""" INVOKE ASP-output "Write" USING BY VALUE FONT-SIZE INVOKE ASP-output "Write" USING BY VALUE """> Welcome to ASP.NET (now in COBOL!) </font> <br> "END-PERFORM.SummaryThe long-term benefits of integration with the Microsoft .NET Framework are that COBOL will more easily adapt to new technologies. New base classes that are developed for the .NET Framework (in any programming language) immediately become accessible to COBOL programmers. The subsequent chapters in this book will explain how the whole Windows 2000 operating system, .NET Framework, and the new Fujitsu COBOL compiler are integrated and how they work together. We will cover the use of the tools in Visual Studio.NET that are used to develop and work with new COBOL applications, as well as migration of legacy code to the new platform. The Visual Studio.NET is not an evolutionary step but a revolutionary one in a software application development platform. We believe that this will represent a revolutionary step in COBOL application development under this environment.