share a variable globally across multiple RPG programs

21 Views


To share a variable globally across multiple RPG programs (Program A, B, C, D) called from a main RPG program, you can use a common data area, a global storage procedure, or a module/service program with exported variables. Here are the most practical solutions for the IBM i (AS/400) environment:


Option 1: Use a Data Area (DTAARA)

Steps:

  1. Create a data area:

    CRTDTAARA DTAARA(MYLIB/MYDATA) TYPE(*CHAR) LEN(100)
    
  2. In the main program, retrieve and store your data into a variable, and then write it into the data area using IN opcode or %DATA BIF:

    DCL-S myValue CHAR(100);
    
    myValue = 'SomeValueFromFile';
    IN MYDATA myValue;
    
  3. In Programs A, B, C, D, read the same data from the data area:

    DCL-S myValue CHAR(100);
    
    IN MYDATA myValue;
    

This is the simplest way to share values globally across programs during a job.


Option 2: Use a Global Variable in a Service Program (Binder)

  1. Create a module with a global variable and getter procedure:

    DCL-S myGlobalValue CHAR(100) STATIC;
    
    DCL-PROC SetValue EXPORT;
      DCL-PI *N CHAR(100); val CHAR(100); END-PI;
      myGlobalValue = val;
      RETURN myGlobalValue;
    END-PROC;
    
    DCL-PROC GetValue EXPORT;
      DCL-PI *N CHAR(100); END-PI;
      RETURN myGlobalValue;
    END-PROC;
    
  2. Create a service program from this module:

    CRTSRVPGM SRVPGM(MYLIB/MYGLOBSRV) EXPORT(*ALL)
    
  3. In the main program, call SetValue('value'), and in A/B/C/D call GetValue() to retrieve it.

This method is ideal if you're using ILE RPG and want better encapsulation and performance.


Option 3: Use USER SPACE (QUSCRTUS API)

  • More advanced and flexible than data areas.

  • Useful for structured or large data.

  • Suitable for sharing across job steps or threads.


⚠️ Things to Note:

  • Data Area and Service Program values are job-specific; if you're running in batch or interactive jobs, the values stay within the job scope.

  • If you're running in a multi-threaded environment, prefer service programs for better control.


Let's go with Option 2: Using a Global Variable in a Service Program — this is the cleanest and most modern approach for ILE RPG Free Form, and avoids external system objects like data areas.


🔧 Step-by-step Implementation

🔹 1. Create a Module with Global Variable (MYGLOBALRPG.RPGLE)

**FREE
ctl-opt nomain;

dcl-s myGlobalValue char(100) static;

// Procedure to set the global value
dcl-proc SetGlobalValue export;
  dcl-pi *n char(100);
    inputValue char(100);
  end-pi;

  myGlobalValue = inputValue;
  return myGlobalValue;
end-proc;

// Procedure to get the global value
dcl-proc GetGlobalValue export;
  dcl-pi *n char(100);
  end-pi;

  return myGlobalValue;
end-proc;

🔹 2. Compile it to a Module

CRTRPGMOD MODULE(MYLIB/MYGLOBALRPG) SRCSTMF('MYGLOBALRPG.RPGLE')

🔹 3. Create the Service Program

CRTSRVPGM SRVPGM(MYLIB/MYGLOBSRV) MODULE(MYLIB/MYGLOBALRPG) EXPORT(*ALL)

🔹 4. Main Program Example (MAINPGM.RPGLE)

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-pr SetGlobalValue char(100) extproc('SetGlobalValue');
  inputValue char(100);
end-pr;

dcl-s myData char(100);

// Simulate data read from file
myData = 'DATA_FROM_FILE';

SetGlobalValue(myData);

// Call next program
callp(e) ProgA(); // assuming ProgA is a bound procedure
*inlr = *on;

🔹 5. Program A Example (PROGA.RPGLE)

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-pr GetGlobalValue char(100) extproc('GetGlobalValue');
end-pr;

dcl-s sharedData char(100);

sharedData = GetGlobalValue();

// Now you can use sharedData
dsply sharedData;

*inlr = *on;

✅ Notes:

  • All programs (Main, A, B, C, D) must include the same GetGlobalValue or SetGlobalValue prototypes and be compiled to bind with the same service program (MYGLOBSRV).

  • Make sure you bind the service program using BNDDIR or directly in the CRTPGM.

 

Post Comments