Highlighted
Absent Member.
Absent Member.
490 views

TypeDef

Can anyone elaborate on using typedefs as opposed to copy replacing.  I don't understand the concept and the little bit of reading that I found does not help much.  Is this a better way of coding working-storage items?  Please provide example code if possible.

Tags (1)
0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: TypeDef

typedefs actually come from other programming languages such as C and C++. They are used to give a more meaningful name to existing types or to create user-defined data types that can be referenced in a usage clause.

A pretty good explanation can be found here:
stackoverflow.com/.../what-is-the-use-of-typedef

A good example of where we use these can be found in the copybook in the Visual COBOL\cpylib folder called cbltypes.cpy.

01 my-buffer typedef.
05 my-name pic x(20).
05 my-city pic x(20).

01 my-buffer1 my-buffer.
01 my-buffer2 my-buffer.

move "Chris" to my-name of my-buffer1
move "Boston" to my-city of my-buffer1
move "Mary" to my-name of my-buffer2
move "Detroit" to my-city of my-buffer1


Doing a similar thing with a copybook would be:

file mybuffer.cpy
05 my-name pic x(20).
05 my-city pic x(20).

then reference:

01 my-buffer1.
copy mybuffer.cpy.
01 my-buffer2.
copy mybuffer.cpy.

move "Chris" to my-name of my-buffer1
move "Boston" to my-city of my-buffer1
move "Mary" to my-name of my-buffer2
move "Detroit" to my-city of my-buffer1

You could define many different typedefs in one file and then bring them in with a single copy statement. When using copy or copy replacing you would have to have a different copybook for each unique record structure you wished to define.


Does this help any?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: TypeDef

Adding on from what Chris has said, with careful use of typedef it could aid you in further expansion of your application at a later stage.

For example you decide to define a StockItem as a Pic x(10) and you have working storage fields, copy books to support this. 5 years down the line you get a request to change the StockItem to a Pic x(15), that could amount to some serious time consuming work.

So you could do this :

77 stockItemTD is typedef pic x(10).

01 stockItem stockItemTD.
01 ws-stock-item stockItemTD.


5 years later I could just change the 77 level to 15 and recompile.

77 stockItemTD is typedef pic x(15).

Neil
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.