Dynamically accessing values of variables

veevers

New Member
Progress is a pretty new language to me. I'm used to many other languages and am having trouble finding how to use progress for dynamic needs.

I am trying to access a variable's value when getting the name of the variable dynamically.

for example: i have a variable named 'firstname'.

i have a list of the fields the user wants to see (and a list of the corresponding variable names).

is there anyway i can have one function that takes the variable name as a string and returns me the value that the variable is holding?

i know i can call a procedure that way, for example:
def var varname as char initial "procname".
run value(varname).

i hope i was clear as to what i need to do. i need a simple solution.

thanks!
 
I don't think you can access Progress variables in the way you would like to...

However, you say you have a "list of fields the user wants to see"; does that mean database fields? If so (and if you are on Progress version 9 or higher) you can use dynamic queries and buffers to access fields and their values dynamically.
 
paul,

thanks for responding.
i didn't think that there was any way to do it. i wish there was.
do you have an example of the syntax for a dynamic query that i can refer to, to see if it will suit my needs?
can it query from more than one table?
thanks!
 
Hi,

You should be abled to do it using include files. You can always do it by generating dynamic program code from within the source program code.

Hope that helps,
 
Let me see (dry typing here without having Progress open at the moment):
Code:
DEF VAR lhQuery AS HANDLE NO-UNDO.
 DEF VAR lhBuffer AS HANDLE NO-UNDO.
 
 CREATE QUERY lhQuery.
 CREATE BUFFER lhBuffer FOR TABLE "<TableName>":U.
 lhQuery:ADD-BUFFER(lhBuffer).
 lhQuery:QUERY-PREPARE("FOR EACH <TableName> NO-LOCK WHERE.....(your where clause goes here)":U).
 lhQuery:QUERY-OPEN.
 lhQuery:GET-FIRST.
 
 DO WHILE NOT lhQuery:QUERY-OFF-END:
   MESSAGE lhBuffer:BUFFER-FIELD("<FieldName>":U):BUFFER-VALUE VIEW-AS ALERT-BOX.
   lhQuery:GET-NEXT.
 END. /* do while not query-off-end */
 
 lhQuery:QUERY-CLOSE.
 DELETE OBJECT lhQuery.
 DELETE OBJECT lhBuffer.
In the above "<TableName>" is the name of the database table and can be replaced with a variable. Same goes for the actual query string in the QUERY-PREPARE and the field name(s) used in BUFFER-FIELD.

HTH
 
hows this

Using 2 levels of indirection this may work for you if the variables are variables and not data fields

first an include file

/*------------------------------------------------------------------------
File : classVar.i
Purpose :
Author(s) : R Lambert
Created : DEC 2004

description : defines a class type variable whose underlying variable should not be directly
accessed. Use get<varName>() or set<varName>(<value>) to assign and retrieve
the data.


Useage :
{classvar.i
&varName = xyz - name of variable
&dataType = charcater|integer|logical|... data type of var
&extent = 5 - extent of variable or do not define it if no extent
&mode = PRIVATE or not defined

}
example
classVar.i defines a character var called Name

setName("fred").

display getName().

----------------------------------------------------------------------*/


&IF DEFINED(EXTENT) <> 0 &THEN
&SCOPED-DEFINE defVarExtent EXTENT {&EXTENT}
&SCOPED-DEFINE localExtent {&EXTENT}
&else
&SCOPED-DEFINE defVarExtent
&endif
&IF DEFINED(mode) <> 0 &THEN
&IF "{&MODE}" <> "PRIVATE" &THEN
&MESSAGE INVALID MODE {&mode} - classvar.i
&UNDEFINE mode
&ENDIF
&ENDIF
&IF DEFINED(varName) = 0 &THEN
&MESSAGE VARIABLE NAME NOT DEFINED - classvar.i
&ENDIF
&IF DEFINED(dataType) = 0 &THEN
&MESSAGE DATA TYPE NOT DEFINED
&ENDIF
DEF VAR _m{&varName} AS {&dataType} {&defVarExtent} NO-UNDO.
FUNCTION set{&FnName} RETURNS LOGICAL {&mode}
(&if DEFINED(localExtent) <> 0 &then INPUT entryNo AS INTEGER , &endif
INPUT ip_m AS {&dataType}) .
&IF DEFINED(localExtent) <> 0 &THEN
IF entryNo > {&localExtent} OR entryNo < 1 THEN RETURN ERROR.
ASSIGN
_m{&varName}[entryNo] = ip_m NO-ERROR.
&ELSE
ASSIGN
_m{&varName} = ip_m NO-ERROR.
&ENDIF
RETURN ERROR-STATUS:ERROR.
END FUNCTION.
FUNCTION get{&FnName} RETURNS {&dataType} {&mode}
&if DEFINED(localExtent) <> 0 &then (INPUT entryNo AS INTEGER ) &endif .
RETURN &if DEFINED(localExtent) <> 0 &then _m{&varName}[entryNo] &else _m{&varName} &endif .
END FUNCTION.

/ *********end of include ***************************** */

suggested way of using it

/**************************************************/


{demoClassVar.i &varName=FirstName &dataType=CHARACTER}
{demoClassVar.i &varName=LastName &dataType=CHARACTER}
FUNCTION getA RETURNS CHARACTER
(cVarName AS CHARACTER):
RETURN DYNAMIC-FUNCTION("get" + cVarName).

END FUNCTION.
FUNCTION setA RETURNS CHARACTER
(cVarName AS CHARACTER
,cValue AS CHARACTER):
DYNAMIC-FUNCTION("set" + cVarName,cValue).

END FUNCTION.
setA("firstName","fred").
setA("lastName","bloggs").

MESSAGE
getA("firstName")
SKIP
getA("lastName")
SKIP
VIEW-AS ALERT-BOX INFO BUTTONS OK.


/**************************************************/


hope this helps
 
Back
Top