Wednesday, January 10, 2024

Enumeration of constants comes to RPG

Enumeration definitions have been added to RPG as part of the Fall Technology Refreshes, IBM i 7.5 TR3 and 7.4 TR9. This allows me to define a list, or group, of constants in one definition structure. Personally I would only include related constants in one enumeration group, but I could include all of my constants, both related and unrelated, in one group.

The definition looks very similar to how to code a data structure definition.

01  dcl-enum EnumerationName ;
02    Name1 'Value 1' ;
03    Name2 'Value 2' ;
04  end-enum ;

Line 01: DCL-ENUM is a new definition type just for enumerations. Each enumeration group must have a name. They can also be qualified, which is how I would use them, more of that later.

Lines 2 and 3: The constant definitions.

Line 4: The end of the enumeration group.

I have a couple of examples of how I think I will use them. In this first example if certain address in the Customer file are in a given sales region I want to give override the address on file and use another, temporary address:

01  **free
02  dcl-s PrintAddress varchar(60) ;

03  dcl-enum TempAddr qualified ;
04    R1 '1215 Guadalupe St, Austin, TX 78701' ;
05    R2 '15 Marilla St, Dallas, TX 75201' ;
06    R3 '406 Caroline St, Houston, TX 77002' ;
07    R4 '100 Dolorosa, San Antonio, TX 78205' ;
08    R5 '500 E San Antonio Ave, El Paso, TX 79901' ;
09    Unknown 'Your region is unaffected' ;
10  end-enum ;

11  dcl-f CUSTOMER ;

12  dou %eof(CUSTOMER) ;
13    read CUSTOMERR ;
14    if not(%eof) ;
15      select REGION ;
16        when-is '1' ;
17          PrintAddress = TempAddr.R1 ;
18        when-is '2' ;
19          PrintAddress = TempAddr.R2 ;
20        when-is '3' ;
21          PrintAddress = TempAddr.R3 ;
22        when-is '4' ;
23          PrintAddress = TempAddr.R4 ;
24        when-is '5' ;
25          PrintAddress = TempAddr.R5 ;
26        other ;
27          PrintAddress = TempAddr.Unknown ;
28        endsl ;
29    endif ;
30  enddo ;

Line 1: You all know me, if I write RPG it has to be modern RPG.

Line 2: Definition for the variable to contain the temporary address.

Lines 3 – 10: The definition of my enumeration group. Notice that I used the QUALIFIED on line 3, this means that I must qualify all of the constants contained within the group with the enumeration group name.

Line 11: The file definition for the Customer file.

Lines 12 – 30: A Do loop to read all the records in the Customer file.

Line 13: Read the Customer file.

Lines 14 – 29: The contents of this If statement will only be performed if the end of the Customer file has not been encountered.

Lines 15 – 28: Here I am using the new version of the Select operation code, with the WHEN-IS test, to test the which region the Customer file record is in move the address from the enumeration group to the PrintAddress variable. As I qualified the enumeration group all of the constants are prefixed with the enumeration group name.

I know that is a simple example. The next one is the same. It shows how I can move an enumeration group into an array.

01  **free
02  dcl-s Array char(10) dim(*auto : 10) ;

03  dcl-enum Colors qualified ;
04    White 'FFFFFF' ;
05    Red 'FF0000' ;
06    Green '00FF00' ;
07    Blue '0000FF' ;
08    Black '000000' ;
09  end-enum ;

10  Array = Colors ;

11  if 'FFFFFF' in Array ;
12    dsply 'Found' ;
13  else ;
14    dsply 'Not found' ;
15  endif ;

Line 2: I have defined this as an auto extending array.

Lines 3 – 9: The enumeration group contains a number of hex codes for colors in HTML.

Line 10: This is wonderful that I can move the entire enumeration group into the array in this one simple statement.

Line 11: I can use an If statement with "IN" to test if the value is in the array.

After compiling this program when I run it I get the following displayed:

DSPLY  Found

Over time I am sure I will come up with more interesting ways of using enumeration groups, but this is post is just an introduction to them.


You can learn more about the changes to RPG's enumeration groups from the IBM website here.


This article was written for IBM i 7.5 TR3 and 7.4 TR9.

1 comment:

  1. I wish the enum syntax would have used a non-blank character as the delimiter between the name and the value, like WHITE : ‘FFFFFF’;


To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.