Tips and Techniques: Using LIKEDS

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

With the advent of OS/400 V5R1, we can use the new LIKEDS keyword on the RPG IV Definition specification to create data structures with the same set of subfields as a previously declared data structure. The LIKEDS keyword is used like this:

D CustAddr_T      DS                  
D  Custno                        7P 0 Inz
D  Addr1                        30A
D  City                         25A
D  State                         2A
       
D Contact         DS                  LikeDS(CustAddr_T)

The first line is the original data structure CUSTADDR_T. I've added an underscore followed by the letter "T" to indicate that this data structure is not being used in the program; rather, it is being used as a template or format to create other data structures.

The last line declares another data structure named CONTACT with the LIKEDS keyword. This new data structure contains the same subfields as the CUSTADDR_T data structure, with the same names.

When LIKEDS is used to declare a new data structure, the new data structure is automatically assigned the QUALIFIED attribute. So the subfields must be referenced using the qualified syntax, as follows:

C                   eval      Contact.Custno = 12345    

If the CUSTADDR_T data structure is nothing more than a template used to declare other data structures, then it might be useful to make that data structure a QUALIFIED data structure. That way, the data structure's subfield names will not collide with any other variable names you've already declared in your source member.

In addition, consider adding the BASED keyword to the data structure template. The BASED keyword causes the compiler to avoid declaring any storage for the field or data structure that uses it. If you add it to the data structure template, then it will not use up any storage. Here's the data structure template again, with both the QUALIFIED and BASED keywords specified.

D CustAddr_T      DS                  QUALIFIED BASED(@)
D  Custno                        7P 0 
D  Addr1                        30A
D  City                         25A
D  State                         2A

Note the "at" sign (@) in the BASED keyword. This symbol is a valid field name in RPG IV. However, it is being used here as an ad hoc pointer variable that is automatically declared by the compiler.

If you need to initialize the subfields of a data structure template, then you can't use the BASED keyword (you can't initialize storage that isn't there).

By default, data structures declared with the LIKEDS keyword do not inherit the initial values of the data structure template. The INZ keyword, however, has also been enhanced to allow you to inherit the initial values. To inherit the initial values of the based on data structure, specify the INZ keyword with a value of *LIKEDS, like so:

D Contact         DS                  LikeDS(CustAddr_T)
D                                     Inz(*LikeDS)

RPG World Is Coming Soon!

For more tips and information like this on RPG IV, join Bob Cozzi along with Jon Paris and Susan Gantner at the second RPG World Conference in Anaheim, November 10 to 12, 2003. Visit www.rpgworld.com for more information and to register.

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 (www.rpgworld.com) and is the author of his own Web site (www.rpgiv.com) and of the RPG ToolKit (www.rpgiv.com/toolkit), an add-on library for RPG IV programmers.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$