× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Any idea if we can use dynamic variable names in RPGLE?

Something like this
fldvalue = %value(&var1);

What you have to understand is that computers don't understand variables. (Not directly, anyway.) When a program is running, it has no notion of variables, it just has addresses in memory.

"Read 4 bytes from address x'123456' and store that in a register. Read 4 bytes from address x'654321' and store that in a register. Add the two registers together. Store the result in memory location x'987654' and so on...

There are no variables. Variables are a device that was created to make life simpler for human beings to write computer programs.

Now, an interpreted language (one that isn't compiled) has variables at run time, because the source code is being translated as the program runs. In an interpreted language, it's relatively easy to have something like varName = 'custno'; varValue = %value(varName). Why? Because it's in the process of interpreting the source code and converting variables into memory addresses ANYWAY... it's no problem for it to do that dynamically.

On the other hand, for a compiled language like RPG, there are no variables anymore. They were discarded when the program was compiled. None of the variable names exist anymore. (with a few exceptions)

Once we chain to a file or create cursor program knows what fields are
available.  Is there any API or Userspace stuff to get the value of the
fields using a dynamic variable name?

Yes and no. One situation where the variable names are stored in the program code is when you have a DBGVIEW() specified. In that case, the variable names are saved in the program so that you can refer to them when you're debugging. Consequently, the debugger APIs would be able to do what you're asking for.

Another situation is when a variable is exported from service program. In that case, the variable's name is made available so that any other (separately compiled) program is able to look up the memory location based on the export name. For that, you can use the QleGetExp() API to get a pointer to the export.

For an externally defined file, you may be able to derive the field value based on the field name because the name is part of the file's external definition. If the fields are loaded from the file into contiguous memory (such as when you use a data structure to load the fields into) then you could use the field's offset in the record as the offset from the start of the user space to set a pointer to the spot where the field lives in memory.

But, other than those times, there's really no way to do what you're proposing, other than re-design RPG from the ground up as an interpreted language.

If you tell us why you need this capability, I'm sure we'll have alternatative ways of solving your problem that don't involve this level of complexity.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.