Description
Microsoft has done an outstanding job documenting the .NET Framework and providing a wealth of examples. The majority of all the examples provided will show a developer in detail how to utilize the Framework or concept in question that is if you're a VB or C# programmer! What about COBOL? While Micro Focus has done an outstanding job of creating numerous (over 100 and growing daily) examples there may still be a time when the right example just doesn't exist yet. In the meantime however, what's a COBOL developer supposed to do to solve a problem you currently have?
In this article we'll take a look at a basic C# sample and then translate the sample into COBOL. This article is intended to be used as a model for you to follow when you run into a C# example and need to convert it to COBOL.
A really neat trick!
While learning the ropes in the .NET area I used a trick someone else told me about when he was learning how to code COBOL in .NET: create a duplicate project in C#. While it sounds like extra work it really is quite simple and quick due to the all built in features of .NET. During your development phase, create an additional project in your solution using either of Microsoft's languages. Choose either VB.NET or C#. I am more comfortable with C# so I use it.
What I do however is create an additional project in our COBOL solution. If I am creating a WinForms application then I create another WinForms application in C#. As I am going through the COBOL development process if I have any questions on how a specific namespace is implemented I use the other language project to help me out. Now don't worry if you don't know C# at this point. You will only be using it to try and help determine what a certain objects method and properties are. In C# (and VB.NET) a feature called 'intelli-sense' has been implemented. Intelli-sense provides a programmer with additional information about an object when they are using that object. We'll get to more of this in a little bit.
Syntactical Differences
C# uses an OBJECT-DOT-PROPERTY syntax. The object in question may be a WinForm, a text box or any of the over 5,000 namespaces within the Framework. The key is when a developer presses the dot (.) . When the dot is pressed a context sensitive window appears showing the developer what options are available for that particular object. The window may contain other objects, properties or methods that are accessible to the object. Instead of remembering where a specific property or method is within an object, by using intelli-sense a developer can 'drill down' to the property or method they are looking for.
For instance, the following code shows intelli-sense for a command button object.
VS.NET is waiting for the developer to select an item or begin typing. When a developer begins typing intelli-sense will position the list based on the input. So instead of keying say 'Text', the developer would begin keying in 'T' and intelli-sense will move the list to show all items beginning with 'T'.
Notice also the presence of tool-tip text to inform the developer of what they are selecting. We are going to modify the text on the face of the command button to be "Call COBOL". To do this we need to select the 'text' property and set it to "Call COBOL". The following image details the code necessary to accomplish this. (We are interested in converting the code to COBOL so we will complete the C# code for you to compare).
Micro Focus COBOL utilizes a different, although similar, syntax to call a method of an object or update its properties. While a more detailed explanation of each verb can be found in the .NET Help file the following can save some time. The syntax used to set a property to a value is
SET OBJECT:: PROPERTY TO SOMETHING.
Notice the double colon (::)? Instead of a single period Micro Focus has adhered to the COBOL standard and implemented the double colon notation to denote a property of an object. The 'TO' completes the syntax for the verb by defining the value the property is to be set to.
The syntax used to invoke a method is
INVOKE "METHOD" USING (any parameters) RETURNING (any parameters).
The items in blue are the syntax elements for the statement. Again, refer to the Help files supplied with Studio for .NET for specific requirements for each item.
Additionally an in-line invocation can be used depending on the return values and this would be coded as:
Set object-reference to OBJECT::"METHOD"(any parameters).
Or
Set cobol-data-item to OBJECT::"METHOD"(any parameters).
Now let's convert our sample. We're going to modify an example provided by Micro Focus so you should already have the base project in the right version. If you accepted the default values when you installed the product the path to the project would be:
C:\Program Files\Micro Focus\Net Express 5.1\Examples\Visual Studio Integration\Forms\WinHello
Conversion
Following the guidelines detailed in the preceding section, to update a button text in COBOL we would code the following:
SET button1::"Text" TO "CALL COBOL".
You'll notice when you begin typing the line above, just as with the C# example, intelli-sense will appear in the same manner and with similar information
The completed method should appear as:
No additional entries in the repository are necessary to access the properties, just as in C#. You should now be able to build your solution and run it.
Wrap-Up
While the sample presented is a simple one, the theory behind it flows through to all of the COBOL for .NET environment. If you follow these simple steps:
1. Research what you are attempting to accomplish
2. Locate the objects, property and method you are attempting to use
3. Create either a C# or VB.NET project, if possible
4. Create the programming necessary to do what you are attempting and see how it is done in C# if no other samples are available
5. Copy the line of code into your COBOL project (commenting it out of course) and then following the above guidelines, translate it into COBOL.
There are numerous other white papers being worked on that will appear soon that go into more detail on this topic. Check the Micro Focus website (http://www.microfocus.com) often for new information.
Over time this process will become second nature to you and soon you'll be coding native .NET calls without doing the research. Remember, one has to learn to crawl before you can run a marathon!
Happy Coding!