Introduction
Declaring namespaces at the top of your Visual COBOL for .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# or the Imports directive in VB.NET. The using and Imports directives permit 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 Visual COBOL for .NET. To do this I will also provide 3 examples. Examples 1 and 2 will show what the code would look like when NOT declaring a namespace. This hopefully will give you a better idea of the advantage of the ILUSING directive. ILUSING and declaring Namespaces is described clearly in Example 3.
NOTE: If you are just interested in is knowing how to declare Namespaces in Micro Focus for Visual COBOL for .NET...feel free to skip to Example 3.
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 one of several pre-defined types in Micro Focus Visual COBOL for .NET. It 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)
*> or use the subtraction "-" operator
set elapsedTime to endTime - 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 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. 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 as in Example 1.
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. The bigger your application is, the use of the namespace(s) throughout the code becomes increasingly redundant. 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 at the very top of the source code.
Line 1 and Line 2 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 Visual COBOL For .NET program are:
- 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.
- 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.
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.