Declaring Namespaces with Micro Focus for COBOL.NET

Introduction

Declaring namespaces at the top of your COBOL.NET code can greatly simplify coding and streamline your syntax by eliminating unnecessary namespaces in your code and the Class Repository. This is identical in concept to the using directive in C#. The using directive in C#
permits the use of types in a namespace so you do not have to qualify the use of a type or class in that namespace.

In this article I will show the use of the ILUSING compiler directive in Micro Focus COBOL.NET. To do this, I will also provide two additional examples of what the code would look like when not declaring a namespace so you can have a better idea of the advantages of this feature. This very basic sample program gets the current time, waits 5000 milliseconds, gets the current time again, and then calculates the elapsed time. In all examples elapsedStr is defined as a String which is a pre-defined type in COBOL.NET that equates to a .NET System.String. i.e. there is no need to declare a namespace for it or use the Type keyword.


Example 1:

Using a Class by declaring it in the Class Repository

The code below uses the Class Repository to declare each class that will be used in the program. Each Class in the repository associates a name (think of it as an alias) to an actual Class.

In this example the Thread class, which is in the .NET Framework System.Threading namespace, is associated with the name ThreadClass.

Likewise, DateTime and TimeSpan, which are both in the .NET Framework System namespace, are associated with "DateTimeClass" and "TimeSpanClass" respectively. In the COBOL application code, it is the alias names that are actually used, not the true .NET type or class names.

 

repository.

class ThreadClass as "System.Threading.Thread"

class DateTimeClass as "System.DateTime"

class TimeSpanClass as "System.TimeSpan"

.

 

working-storage section.

01 begTime DateTimeClass.

01 endTime DateTimeClass.

01 elapsedTime TimeSpanClass.

01 elapsedStr String.

procedure division.

set begTime to DateTimeClass::"Now"

invoke ThreadClass::"Sleep"(5000)

set endTime to DateTimeClass::"Now"

set elapsedTime to endTime::"Subtract"(begTime)

set elapsedStr to elapsedTime::"ToString"()

 

 

While the Class respository makes it convenient to declare individually each Class or type that will be used in your program. It is a convention that .NET programmers in general would not be familiar with. Furthermore, if a COBOL programmer were to look at a C# or VB.NET program as an example of how to do something they would also see that this convention is not is not used and is not necessary in other .NET languages. It could also lead to using non-standard or inconsistent class and type names throughout your application code. This could make it a little more difficult for a .NET programmer to maintain the code later...i.e. always having to look in the Class Repository to see which class is actually being used.(Classes and Enumerators) to be consistent, but this is also somewhat redundant b. The actual program code would then also be using Class names or aliases that are not standard. If another person later had to maintain this code, they would always have to look in the Class Repository sections to confirm which actual type is being used.

 

Example 2:

Using a Class without declaring it in the Class Repository or declaring a Namespace

 

The code below uses Classes or Types without having to declare them in the Class Repository. However, because no namespace was declared, the fully qualified type including the namespace is required. Notice the use of the type keyword followed by the fully qualified type (including the namespace) to specify a type in the working-storage section or in the code.

In this example the Thread class or type, which is in the .NET Framework System.Threading namespace, is fully qualified everywhere it used by specifying System.Threading.Thread.

Likewise, DateTime and TimeSpan, which are in the .NET Framework System namespace, are also fully qualified as System.DateTime and System.TimeSpan respectively. In the COBOL application, the actual fully qualified class or type names are used including the namespace that contains the type...NOT names declared in the Class Repository.

 

working-storage section.

01 begTime2 type "System.DateTime".

01 endTime2 type "System.DateTime".

01 elapsedTime2 type "System.TimeSpan".

01 elapsedStr String.

procedure division.

set begTime2 to type "System.DateTime"::"Now"

invoke type "System.Threading.Thread"::"Sleep"(5000)

set endTime2 to type "System.DateTime"::"Now"

set elapsedTime2 to endTime2::"Subtract"(begTime2)

set elapsedStr to elapsedTime2::"ToString"()

 

The downside to this technique is that it can get rather cumbersome while coding if you always have to specify the fully qualified class or type including the namespace. This takes us to our final example and the purpose of this How-To article.

 


 

Example 3:

Using Classes contained in Namespaces that have been declared

The code below uses Classes or Types without having to declare them in the Class Repository. However, the difference between this and example 2 is the namespaces that contain the types have been declared with the ILUSING directive. Because of this, it is not necessary to fully qualify the types in the COBOL data definitions or business logic. The type keyword is still required, but you only need to specify the Class or Type name in double quotes.

In this example the Thread, DateTime, and TimeSpan classes are used without their respective namespaces in the code. Their namespaces are declared once at the top of the program with the ILUSING directive. This is identical in concept to the USING directive in C# or IMPORTS in VB.NET. $set is a feature of the Micro Focus compiler that allows you to set compiler directives directly in the source code. In this example it is used to specify the ILUSING directive.

 

1st lines of the COBOL program or Class ($set begins in column 7):

$set ilusing"System"

$set ilusing"System.Threading"

 

working-storage section.

01 begTime3 type "DateTime".

01 endTime3 type "DateTime".

01 elapsedTime3 type "TimeSpan".

01 elapsedStr String.

procedure division.

set begTime3 to type "DateTime"::"Now"

invoke type "Thread"::"Sleep"(5000)

set endTime3 to type "DateTime"::"Now"

set elapsedTime3 to endTime3::"Subtract"(begTime3)

set elapsedStr to elapsedTime3::"ToString"()

 

The advantages of declaring the namespaces of the types or classes used in your COBOL.NET program are:

1.    Promotes streamlined coding by allowing the use of types or classes with their exact names...without having to fully specify the namespace in the name everytime those classes are used.

2.       Eliminates the need to use the Class Repository to specify each individual class and type used in your program. The combination of the ILUSING directive and the Type keyword allow you to specify the exact names of types directly in your code.

3.    Because naming is more consistent and standardized, it makes the classes and types in use more recognizable to other .NET programmers looking at your code.

 



About the Author

Michael Bleistein

Mike is a Solution Architect for Micro Focus in North America. In a nutshell he helps paint the vision and architect solutions for customers by articulating how a combination of Micro Focus and industry technologies can provide solutions to their critical Enterprise Application requirements. Toward this overall goal, he provides numerous Whiteboard and Live Meeting sessions as well a Proof of Concept efforts. He has also worked closely with every release of the Micro Focus COBOL for .NET technology since its initial release several years ago.

Downloads

Comments

  • Discount Oakley Mens no tax worldwide

    Posted by pyqkdjarr on 06/29/2013 12:19pm

    Cheap oAkLey sunglasses ,Oakley sunglasses are a same type of tacit understanding between you as well as possess a multi-functional and their own fashion eyewear glasses produced? The planning of Oakley sunglasses, the principle motive behind is the best hole to make sure the effective protection your eyes, is in charge of providing advanced color around care. Fake Ray Ban ,For style forward even Oakley at the very top. Oakley standby time with the latest model generated glasses. Oakley shade of the application is quite an enormous celebrity list. Not just will be the name of Oakley and well-designed clothing, this is known for a range of other products. Nearly many celebrities like them, they can be purchased for virtually any consumer. OAkleY GAscAn ,To make certain colors employ a relatively high-sounding goblets with our new little brown eyes, with James, and Lewis to exclude the sun's shadow is normally perhaps the organization varieties. Cheap Oakley sunglasses designer chic sunglasses during the warm months, that has become a necessary fashion accessory, this helps make the world's great fashion. Bulky sunglasses cover the majority of the section of ??the face area, you'll be able to adjust the straight slender jaw and cheekbones and turn into the first choice if you buy. It is possible to select a black frame, gold, guns, brown or ivory. Flexible flexible frame as to the you can sustain your ears behind the lens and for this framework resized. Oakley come with an interchangeable lens design enables you to accommodate the changing light and optimize performance in just about any environment. Cheap Oakley sunglasses, folks have the ability to quickly conform to their vision, and present every possible advantage. Oakley anti-aircraft guns jacket XLJ There are several types so that you can choose! Oakley sunglasses are great males and females, brand sunglasses, stylish and useful, many people as it, like to wear after giving a sense of security. Destruction on the rays of the sun affect them is in fact seniors, small children. When compared to child's fixing their gaze with broadcasting plus the elderly, greater the chance of light array. Oakley sunglasses (female) advantages from its special history, almost all of them wanting to imitate them. Hardly any women simply get the glasses, they find what they want. The pilot sunglasses is probably the most in-demand lately in men and ladies styles. Oakley sunglasses, a style of this style, the regular brown shadows, they could almost match any equipment.

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds