There always seems to be an endless supply of oversights when writing code, and RPG IV is no exception. When I do code reviews, I often see the following problem in the Definition specification. Can you spot the problem?
D ITEM DS Inz
D Category 3A
D SeqNbr 5S 0
D Postfix 2S 0
D AmtDue 7P 2
The problem is that the field named AMTDUE (line 5) was not intended to be part of the ITEM data structure. In order for AMTDUE to be an independent field, the field must be declared as a standalone field. This requires an "S" in position 24 of the Definition specification. Here's the corrected code:
D ITEM DS Inz
D Category 3A
D SeqNbr 5S 0
D Postfix 2S 0
D AmtDue S 7P 2
Remember, a data structure or parameter list ends when it encounters another type of Definition specification. This means a statement with an entry in column 24 ends the prior Definition specification group. A Definition specification group is also terminated when an Input, Calc, or Output specification is encountered.
Knowing the LIKE Keyword
The LIKE keyword can be used on the Definition specification to create a new field like an existing field. What some people do not know, however, is that when the LIKE keyword is used, only the field's data type and length are inherited. Other keywords or attributes that apply to that field--such as the DIM keyword, an initial value, or date format--are not inherited. Therefore, when you use the LIKE keyword and you also want the new field to be an array or have the same initial value, you must specify those keywords as well as the LIKE keyword. Here's an example:
D CubsScores S Like(GameScores) Dim(%elem(games))
This creates two fields, GAMES and CUBSSCORES. Both are arrays with 10 elements each, and each element is a seven-position packed value with zero decimals.
Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language --the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.
LATEST COMMENTS
MC Press Online