IBM i > DEVELOPER > RPG

Data Structure Enhancements in V5R1 and V5R2


Over the last few years we've seen massive changes in the RPG language. Subprocedures and prototyping opened new methods of modularizing and calling our code. Date fields and the associated op-codes and BIFs allowed us to do in one line of code what had previously taken 40 or 50. Dynamic storage allowed us "grow" arrays as required. Pointers opened the door to many C functions that were previously off limits to the RPGer. And the list goes on. The one thing that other languages like C and Java offered programmers that RPG lacked (until now) was the ability to define our own data types.

Suppose you needed to deal with several addresses in a program. Each address contains several subfields: Street Address, City, State, ZIP, etc. Wouldn't it be nice to define a DS that was more like a data type-that is, you could simply declare another DS that had subfields exactly like "Address." Well, as of V5R1, you can.

D Address         DS
D   Street                      30a
D   City                        20a
D   State                        2a
D   Zip                          5a
D   ZipPlus                      4a

D MailAddr        DS                    LikeDS(Address)
D ShipAddr        DS                    LikeDS(Address)

In this example, we've defined MailAddr and ShipAddr to be like (LIKEDS) the address structure. Therefore, both MailAddr and ShipAddr contain subfields named Street, City, State, etc. It's important to that the subfields have the same name, but may contain different data values in each DS. Yes, two fields with the same name contain different data? Can this be RPG? This is another first for RPG -- qualified data names. As you can see in the example below, because we've defined MailAddr and ShipAddr using LikeDS, we can (must, in fact) qualify the names of the subfields in them.

C                   Eval      PrintCity = MailAddr.City + ', '
C                                         + MailAddr.State + ' '
C                                         + MailAddr.Zip
C                   If        MailAddr.ZipPlus <> *Blanks
C                   Eval      PrintCity = PrintCity
C                                         + '-' + MailAddr.ZipPlus
C                   EndIf

(Note: If we had needed to refer to the subfields in the original DS (Address) in the logic, we would be able to add the QUALIFIED keyword to the Address DS. Then we could refer to Address.City or Address.State. The Qualified keyword is implicitly defined for a DS containing the LIKEDS keyword.

 

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.


comments powered by Disqus

Advertisement

Advertisement

2017 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters