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

Posted on 2010-09-23
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 100 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

Technology Partners: 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 400 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 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

679 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