Renier
Renier

Reputation: 1830

How do you define a global variable in a rpg Program that can be accessed in calling functions

RPG on the AS400

Is their a way to define a Global variable that can be accessed by functions inside my main function?

For example I have my main rpg program (Lets call it "MAIN_PROGRAM", inside it I call another RPG program(Lets call it SUB_PROGRAM). Now I want to define a string variable "ABC" in my MAIN_PROGRAM and then when calling my SUB_PROGRAM use the variable inside my SUB_PROGRAM.

The reason I don't want to send it into SUB_PROGRAM as a input parameter is that my MAIN_PROGRAM is compiled with a generator (CA-PLEX)... and this gives me a limmit to a string size of 60 000. I did get a way of going around this but cant pass it to the SUB_PROGRAM because PLEX will cut it off at 60 000 characters.

What I would like to do is to create a Global varying variable that can be referenced by the SUB_PROGRAM.

I am new to RPG programming. I am able to edit the source of the programs but I just don't know if one can create a global variable or something similar.

Upvotes: 3

Views: 3208

Answers (2)

Barbara Morris
Barbara Morris

Reputation: 3674

You could export the variable from a service program and bind both programs to the service program. You would import the variable into the programs. As long as the programs are in the same activation group and the service program is *CALLER, or it is in the same named activation group, then the import variables in both programs will access the same exported variable in the service program.

You have to be very careful to define all the IMPORT and EXPORT variables the same. The way to do that is to define the variable in a /copy file like this:

dcl-s shared_variable varchar(1000000)
     /if defined(do_export)
         export
     /else
         import
     /endif
   ;

srvpgm module source:

ctl-opt nomain;
/define do_export
/copy copyfile

Source for the programs:

/copy copyfile
shared_variable = 'abcde';
if shared_variabe = '12345';
   ...

Another approach is to define the variable in the service program without the EXPORT keyword, and use procedures to get and set the value. This seems more complicated that using IMPORT/EXPORT, but it can be easier in the long run since it can be difficult to debug problems caused by sharing variables using IMPORT/EXPORT. For example, if you don't want the variable to be used before it has been explicitly set to a value, your "get" procedure could handle that.

Copy file:

dcl-s shared_variable_t varchar(1000000);
dcl-pr set_shared_variable;
    val like(shared_variable_t) const;
end-pr;
dcl-pr get_shared_variable like(shared_variable_t) end-pr;

Srvpgm module source:

ctl-opt nomain;
/copy copyfile
dcl-s g_shared_variable like(shared_variable_t);
dcl-proc set_shared_variable export;
   dcl-pi *n;
       val like(shared_variable_t) const;
   end-pi;
   g_shared_variable = val;
end-proc;

Source for the programs:

/copy copyfile
set_shared_variable ('abcde');
if get_shared_variable () = '12345';
     ...

Upvotes: 7

Charles
Charles

Reputation: 23793

Between two programs? No.

Between two modules bound into the same *PGM (or *SRVPGM) object, you can use the EXPORT and IMPORT keywords on the variable d-spec.

If you must leave them as programs, passing the data as an external data area may be your best choice.

What version of Plex are you using? I haven't used it since 6.1...I'd be somewhat surprised if the current version hasn't kept pace with RPGLE.

If you haven't already, the CA Plex / 2E community is a great resource for help.

Upvotes: 4

Related Questions