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:

  • 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?
  • 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
  • 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