How to access to a global var which name is on a string variable ?

Dear Expert:

I would like to access to a global var which I don't know its name in compilation time. I have a string variable containing the name of the global var, and I would like to access to the contents of this global var which is known at run-time.

I found GetGlobalVarID and GetLongGlobalVar functions which I feel its exactly what I need, but I don't know how to use them. Can you provide me an example ?

Thank you.
LVL 15
Who is Participating?
From this explanation I didn't get why you store global variable name in string as explained in original question.

What I understood is you have scenario something like this...
you have one window which allows to open different windows as response. And you want to pass one variable or value (e.g. x=2) from Main to first child window (window A) and do some calculation on it and as a result it changes to (x=4) and again pass this value to second child window (window B) and use x=4 for some calculation. And hence X is declared as global variable so that once it's value is changed from child window A or B it's available to main window.

If this is what you are doing then you no need global variable. X can be declared as instance variable in main window and can be passed to called window (A or B) using OpenWithParm. In OpenWithParm you can pass single variable or multiple (as structure) or any other PB object. And in called window (A or B) you can get this value using message.stringparm, message.doubleparm, message.longparm or message.powerobjectparm.

Similary when you close window A or B, you can return modified value of the same variable to main window by closewithreturn.

Please read PB help for OpenWithParm, CloseWithReturn and Message object for more detail and let me know if you need further help.

Do you which datatype will global variable have?

gplanaAuthor Commented:
most of them are long.
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Hello, gplana,

I don't know anything about PowerBuilder, but in VB dotNet if you know the module in which the variable is declared ("Module1" in the example) then you could use statements like shown in the attached snippet.  

It seems to me that it should be possible to access global variables without knowing where they are declared, but I don't know how to do this.  Perhaps someone else can help with that.

I hope this gives you some idea or at least a starting point from which to search.


        Dim strVarName As String = InputBox("Enter variable name")
        Dim fdiFound As Reflection.FieldInfo = GetType(Module1).GetField(strVarName)
        Dim lngValue As Long = CLng(fdiFound.GetValue(Nothing))
        MsgBox("Value of " & strVarName & " is " & lngValue)
        Dim lngNewValue As Long = CLng(InputBox("Enter new value"))
        fdiFound.SetValue(Nothing, lngNewValue)
        MsgBox("New value of " & strVarName & " is " & CLng(fdiFound.GetValue(Nothing)))

Open in new window

gplanaAuthor Commented:
This is exactly the idea. Unfortunatelly, I have to program using PowerBuilder, not .Net, so I cannot use this example.
I know there are two functions in PowerBuilder called GetGlobalVarID and GetLongGlobalVar. In theory the first one gets the reference to the variable, and the second one gets the long value of the reference.
Unfortunately I haven't found any example of use for these functions, and when I try to call to thes functions compiler seems not to know them, so I suppose I have to add some library, but I don't know what.
Thank you any way for your help.
GetGlobalVarID and GetLongGlobalVar can only be used with IPB_Session interface. And ifI remember correctly, IPB_Session is to create powerbuilder extension to allow external applications like C++ and Java to call Powerbuilder functions.

Not sure if you can use that functionality here in your case.

It would be more easier to understand your required, if you provide an example. May be there is some other alternate for that in PB.

gplanaAuthor Commented:
I'm using global vars to pass some data to windows. This is like a parameter on a function, but used by a window. For example, I use a globar var fr knowing the user that logged on the application,
Most of our windows are response type, so after callikng open I cannot call any function on the window (this would be a better way to pass these parameters to the window.

Also, I use global vars for letting know to the calling window what item have user selected on the called window.  This is, I'm using also global variables for returning values from the window.
Is there a better way for doing that ?
gplanaAuthor Commented:
You are right, but I have some .pbl libraries with these windows, which are included on some projects (so I'm sharing the .pbl). The problem is every project has its own global vars and windows. I have solved the windows problems by using the attached code. I need something similar for global vars.
However, I didn't know OpenWithParm and CloseWithReturn functions, which I read its help tomorrow.

Window w

if i_am_on_project_1 then
end if

Open in new window

gplanaAuthor Commented:
It seems it's impossible to access dinamically (on run-time) to a global var, but OpenWithParm and CloseWithReturn is a very good way to solve what I need.

Thank you all of you for youc collaboration.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.