Click here to Skip to main content
Click here to Skip to main content
Go to top

Using Arrays with Micro Focus COBOL.NET

, 8 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Micro Focus COBOL.NET Syntax for working with .NET System.Array + examples

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 at 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 data. This classic COBOL syntax is still very much supported in Micro Focus COBOL.NET, but the data structures it supports should not be confused with the .NET System.Array class.

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 TYPEoccurs [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 through 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 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 Studio 2008.

The soon to be released Micro Focus Visual COBOL (shortly after the Microsoft Visual Studio 2010 launch) simplifies and improves syntax for COBOL and .NET. This will be covered in future articles!

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 Decimal array
  • The C# class changes the first occurrence 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 through 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"() 
set SecStr     to new "SecureString"() 
perform varying CharVal thru CharArray 
    invoke SecStr::"AppendChar"(CharVal) 
end-perform

Example 4

  • Stores the 3 Arrays above into one Array of Arrays with set content of…
  • Implicitly sets the size to 3
  • Essentially shows the flexibility of the types of things you can store in an Array
  • One of several ways to create a “Jagged Array”
set content of ArrayofArrays to (DecimalArray StringArray CharArray)

Example 5

  • Sets the sizes of a Jagged Array so that you have an Array of 2 Arrays.
  • One Array has a size of 3 and the other has a size of 5
  • Sets the value of the 3rd item in the 2nd Array to the current date/time
set size of DateArrays to 2   
set size DateArrays(1) to 3 
set size of DateArrays(2) to 5 
set DateArrays(2 3) to type "DateTime"::"Now"

Micro Focus COBOL.NET Article02.cbl

      $set ilusing"Article02CSharp"   
       program-id. Article02 as "Article02.Article02". 
       data division. 
       working-storage section. 
       01 DecNum        pic s9(5)v99 comp-3 value 123.45. 
          *> Traditional COBOL packed       decimal field 
       01 DecimalArray  Decimal Occurs any.               
          *> Array of System.Decimal 
       01 StringArray   String occurs 3.                  
          *> Array of System.String with initial size of 3 
       01 CharArray     Character Occurs any.             
          *> Array of System.Character with no initial size 
       01 PswdString    String.                           
           *> .NET System.String 
       01 CharVal       Character.                        
           *> .NET System.Character 
       01 SecStr   Type "SecureString".                   
           *> System.Security.SecureString 
       01 ArrayObj Type "MyClass".                         
           *> A data item of type Class1 C# class in this sample 
       01 idx           binary-short value zero.          
           *> .NET Int16 or Short 
       77 ArrayofArrays Type "Array" occurs any.          
           *> An Array of DateTime instances 
       77 DateArrays Type "DateTime" occurs any occurs any. 
           *> A Jagged Array of DateTime objects 
       procedure division. 
      *>   Example 1 
           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 "MyClass"() 
           invoke ArrayObj::"ProcessArray"(DecimalArray) 
      *>   Example 2 
           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 
           set PswdString to "mypassword" 
           set CharArray  to PswdString::"ToCharArray"() 
           set SecStr     to new "SecureString"() 
           perform varying CharVal thru CharArray 
               display CharVal 
           end-perform 
      *>   Example 4 
           set content of ArrayofArrays to (DecimalArray StringArray CharArray) 
      *>   Example 5            
           set size of DateArrays to 2   
           set size DateArrays(1) to 3 
           set size of DateArrays(2) to 5 
           set DateArrays(2 3) to type "DateTime"::"Now" 
           goback. 
       end program Article02.

C# MyClass.cs

using System; 
namespace Article02CSharp 
{ 
    public class Class1 
    { 
        public void ProcessArray(Decimal[] myArray) 
        { 
            myArray[0] = 543.21M; 
        } 
    } 
}

History

  • 8th March, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Bleistein
Micro Focus
United States United States
Mike is a Sr. 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.

You may also be interested in...

Comments and Discussions

 
GeneralGreat post Pinmemberalex turner9-Mar-10 22:14 
GeneralRedo the formatting... PinmvpDave Kreskowiak9-Mar-10 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 8 Mar 2010
Article Copyright 2010 by Michael Bleistein
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid