Tips and Techniques: Miscoding Standalone Fields

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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?





.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++
     D ITEM            DS                  Inz
     D  Category                      3A   
     D  SeqNbr                        5S 0 
     D  Postfix                       2S 0 

     D AmtDue                         72

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:

.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++
     D ITEM            DS                  Inz
     D  Category                      3A   
     D  SeqNbr                        5S 0 
     D  Postfix                       2S 0 

     D AmtDue          S              72

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 Games           S              7P 0 Dim(10)
D CubsScores      S                   Like(GameScoresDim(%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.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: