Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL Challenge in C# 1

Status
Not open for further replies.

hhinman

Technical User
May 11, 2003
11
US
I am trying to convert COBOL code to C#. One of the greatest challenges in general is revolves around the fact that COBOL is a loosely typed language as compared to C#.

In COBOL you can define an I/O buffer to read and write to an external file as effectively a byte array (e.g. records being read and written may contain mixed data types - strings, integers, etc).

A program may read a 100 byte record into this buffer and no type checking takes place.

The program may then move this data to the equivalent of a C# structure with a single simple statement like:

MOVE IO-BUFFER TO MY-STRUCTURE.

What this effectively does in pick of the 100 byte IO-BUFFER and lay it into the structure from the beginning of the Structure without doing a member by member move. The reason this works in COBOL is that COBOL allocates and maintains fixed length areas in memory for each item in a structure and does not perform type checking when moving data into a variable. SO it effectively treats the structure as a byte array and simply moves one byte array to another. It is taken for granted that the data will always be in the correct format with fix length items that cause the move ot line up with the individual field types.

Any idea how to do this in C# without having to write a special routine for each move to break the IO buffer up and do a field by field move into a structure?
 
AFAIK, your problem here is that C# stores arrays outline (that is as a pointer to the array not the array itself), so even though you can do what you require if all the fields are simple numeric types there's no simple way to do it with strings.

if you're really stuck on keeping this functionality, though, it's pretty easy in c++: a swift dll with the desired call turning your byte array into a struct shouldn't be too hard to write.

but that's a question for another forum.


mr s. <;)

 
Thanks for the response. I need to stay in C#. The real problem here is that the assignment operation in COBOL (the "MOVE" statement) Is the most commonly used statement in COBOL. Structures and sub-structures are also very prevalent in COBOL programs. So this means potentially having to write hundreds of specialized methods just to move data from one place to another just to emulate this behavior. I would really like to find the best way (e.g. using Reflection or and/or Marshalling) to simplify this and also not drag performance down.

Anyone have any ideas?
 
you'll need to translate your COBOL structures into C# classes. you can do this automatically with a little ingenuity.

all you need to add to the code beyond this is a constructor which takes a byte array and populates the private members.

it's been nearly twenty years since i did any COBOL (yes, yes) so i can't remember any syntax. i'll just make some up. please imagine the following starts in column eight if that make you happier.

Code:
[COLOR=green]// METACODE:[/color]
struct:
 char[10] f1;
 int16 f2;
 int16 f3;
 char[12] f4;

Code:
public class FromCOBOL
{
 string f1 = string.Empty; //  0 -  9
 Int16 f2;                 // 10 - 11
 Int16 f3;                 // 12 - 13
 string f4 = string.Empty; // 14 - 25

 public FromCOBOL(byte[] byteArray)
 {
  for ( int i = 0; i < 10; i++ )
  {
   f1 += byteArray[i].ToString();
  }
  f2 = 256 * byteArray[10] + byteArray[11];
  f3 = 0; // floats you're on your own
  for ( int i = 14; i < 26; i++ )
  {
   f4 += byteArray[i].ToString();
  }
 }
}

if you have floats in your structs, look at for a shoehorn of a union-like struct.

good luck,


mr s. <;)
 
Thanks for the link, misterstick, I've been looking for something like that since .Net first came out - I frequently use Variant Records in Delphi (the equivalent of Structs with Unions).

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top