Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2010-09-23
Medium Priority
Last Modified: 2013-12-26
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.
Question by:gplana

Expert Comment

ID: 33777162
Do you which datatype will global variable have?

LVL 15

Author Comment

ID: 33777872
most of them are long.
LVL 12

Assisted Solution

omegaomega earned 400 total points
ID: 33790020
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Author Comment

ID: 33790193
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.
LVL 14

Assisted Solution

sandeep_patel earned 1600 total points
ID: 33815423
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.

LVL 15

Author Comment

ID: 33815486
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 ?
LVL 14

Accepted Solution

sandeep_patel earned 1600 total points
ID: 33817824
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.

LVL 15

Author Comment

ID: 33817887
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

LVL 15

Author Closing Comment

ID: 33860404
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question