Introduction
The .NET System.Array class has many uses and is often encountered when you interact with other .NET classes and even user developed C# or VB.NET classes.
In this article I will show the syntax for using Arrays in Micro Focus COBOL.NET as well as some examples of usage. Once you know this syntax you can then:
-
Define and initialize .NET arrays in COBOL.NET
-
Pass Arrays to other .NET methods that require a System.Array parameter
-
Receive and process Arrays in your methods that are invoked from other Classes.
Most importantly you will be able to very easily use .NET Arrays as any other .NET language would. The System.Array class along with members, methods, and properties is completely documented on Microsoft's MSDN site: http://msdn.microsoft.com/en-us/library/system.array.aspx. Again, the purpose of this article is to provide information on using the Array class with Micro Focus COBOL.NET.
If you are a COBOL programmer, you know that COBOL has had syntax since its beginning to create something similar to an array. Using the OCCURS keyword allows COBOL programmers to create a table and even nested tables of simple to very complex structures of ![]()
However to support .NET Arrays as well as interact with other .NET classes and languages, Micro Focus has extended the syntax of OCCURS. One key difference (among others) between .NET Arrays and OCCURS tables in "traditional" COBOL is that OCCURS tables in "traditional" COBOL are either fixed size or variable with a maximum size. In either case the size is essentially established at compile time. Aside from not being limited by the fixed nature of OCCURS tables, Arrays bring a number of other capabilities including a rich set of methods and properties that are very easy to use in COBOL.NET.
.NET Arrays can be created as fixed size or dynamic. Even Arrays created initially as fixed can be resized as needed at runtime.
Micro Focus COBOL.NET supports the creation of both types of Arrays. Here are some samples of Single and Multi-Dimensional Arrays:
To create an array of Strings with an initial size of 5:
01 myString1 String occurs 5.
To create an array of Strings with no initial size:
01 myString2 String occurs Any.
To create a multi-dimensional (in this case 2 dimensions) Array of Decimals:
01 myDecimal Decimal occurs 5 10
To Create a Jagged Array (An Array of Arrays)
01 myDates type "Datetime" Occurs 10 Occurs Any.
Note the use of the new keyword Any. The definition of the Array has the following general format:
01 dataname TYPE occurs [size | Any]
The Type specified determines what type of items the Array will hold. The size is an integer that specifies the initial size of the Array. If Any is used, the array will then be initialized later by setting the size of it at runtime using the set size of syntax, initializing it with items using set content of syntax, or by setting it to another array of the same type (Decimal, String, Class Type, etc).
The dataname must be a top level item of either 01 or 77 and Type must be any .NET data type or class. If you need to store traditional COBOL PIC items or groups of items in an Array, the recommended approach is to define a class which contains that data and then specify the Class as the type. Alternatively you can look at using .NET Collections which is a whole other topic.
Once an Array is created in Micro Focus COBOL.NET, it can be accessed in several ways and all of the System.Array methods and operations are supported in Micro Focus COBOL.NET and other .NET languages because it IS a .NET System.Array. From a COBOL syntax standpoint all Arrays begin at index 1. This is purely a syntax difference with C# and other .NET languages where the first index position is zero. If you pass a Micro Focus COBOL.NET Array to a C# class, C# would still access the first position with an index of zero. Likewise, if the Array is created in C#, Micro Focus COBOL.NET would access the first position with an index of 1. The Arrays work as expected in each language.
To iterate thru an Array similar to the C# foreach statement, you use:
"perform varying object thru ArrayObject".
This allows you to process a number of COBOL statements for each item in the Array without the need for an index. Example 3 uses this technique. If you have an Array of Strings then object would be a data item defined as a String. Through each iteration, the Array item is moved into object and then can be processed by subsequent COBOL statements. In this case we are using an Array, but actually the perform varying syntax can be used against any instance of a .NET Collection class.
I have attached a sample solution with this article and have also included below the source of the 2 modules. Here are 5 examples of Array usage in Micro Focus COBOL.NET. Hopefully these should provide a good reference of different techniques and also serve as reference for the syntax needed in different scenarios when using .NET Arrays. The samples were coded in Micro Focus COBOL.NET in Visual ![]()
The soon to be released Micro Focus Visual COBOL (shortly after the Microsoft Visual ![]()
Example 1
-
Set the size of a Decimal Array
-
Initialize the array with Decimal data (comp-3 gets mapped to Decimal to demonstrate use of existing COBOL data) using the Array length property in a perform loop
-
Instantiate a C# class and pass it the Deciml array
-
The C# class changes the the first occurance and returns
set size of DecimalArray to 5
perform varying idx from 1 by 1 until idx > DecimalArray::"Length"
move DecNum to DecimalArray(idx)
add 100.50 to DecNum
end-perform
set ArrayObj to new "Class1"()
invoke ArrayObj::"ProcessArray"(DecimalArray)
Example 2
-
Initializes the Array with data and implicitly initializes its size to 3
-
Resize an array with the Array Resize method - increasing its size by 10
-
Micro Focus COBOL.NET "set size of" can also be used to resize an Array but will reinitialize the contents as well.
set content of StringArray to ("Str1" "Str2" "Str3")
invoke type "Array"::"Resize"[String](StringArray, StringArray::"Length" + 10).
set size of StringArray to 20
Example 3
-
Creates a Character Array from the ToCharArray method of the String Class
-
Use the "perform varying" syntax to iterate thru the Array an object
-
Uses the AppendChar method of the SecureString class to build a .NET SecureString
set PswdString to "mypassword"
set CharArray to PswdString::"ToCharArray"()

Asfend YarPosted Feb 29, 2016, 1:36 PM
good