Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » COBOL.NET » .NET Arrays with Micro Focus COBOL.NET

.NET Arrays with Micro Focus COBOL.NET

Overview of how to use the System.Array class with Micro Focus COBOL.NET. Covers the syntax needed to create and use Arrays. Includes examples and sample code

Page Views : 3237
Downloads : 15
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
COBOLArrays.zip
 
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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 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 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 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 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"()

           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 MyClass

    {

        public void ProcessArray(Decimal[] myArray)

        {

            myArray[0] = 543.21M;

        }

    }
}

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Michael Bleistein
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.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.