Midrange News for the IBM i Community


Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Chicagoland
Larger RPG Field Lengths - How To
has no ratings.
Published: 19 May 2015
Revised: 19 May 2015 - 861 days ago
Last viewed on: 26 Sep 2017 (1640 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Larger RPG Field Lengths - How To Published by: Bob Cozzi on 19 May 2015 view comments

If you're a software vendor or a regular IBM i programmer that supports back-releases of the OS along with the current release, you may be coding for the LCD (lowest common denominator) when you declare variables.

With conditional compiling, I've been able to (A) avoid LCD and (B) automatically have my code use the current features when compiling for a newer release. here's how:

 

     D pXMLStmt        S               *
      /IF DEFINED(*V7R1M0)
     D xmlStmt         S               A   Len(2048000)
     D/ELSE
     D xmlStmt         S          65535A
     D/ENDIF
     D                                     Based(pXMLStmt)
      /free
             pXMLStmt = %alloc( %size(xmlStmt) );
      ... Do more normal stuff here.
            dealloc pXMLStmt;

You do NOT need to use %ALLOC. I used it here to give a more complex example. Using normal (non-based) field declarations works just as well. The key here are the two features that some people overlook.

  1. Use conditional compiling to control which version of a field is declared and how big it is.
  2. Use %SIZE(myVar) within the calc specs to control any reference to the length of the field.

Other than that you can pretty much do whatever you want.

Note that on v7.1 the max length of an RPG field is 16mb. Prior limits (in reverse order) were:

  • 64k
  • 32k
  • 256 characters

 

A cooler way to do the above would be the use the D spec continuation symbols and avoid redundant field name usage. As I illustrate below:

 

     D xmlStmt...
      /IF DEFINED(*V7R1M0)
     D                 S               A   Len(2048000)
     D/ELSE
     D                 S          65535A
     D/ENDIF

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS