Solved

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

Posted on 2010-09-23
10
688 Views
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.
0
Comment
Question by:gplana
10 Comments
 
LVL 2

Expert Comment

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

0
 
LVL 15

Author Comment

by:gplana
ID: 33777872
most of them are long.
0
 
LVL 12

Assisted Solution

by:omegaomega
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.

Cheers,
Randy

        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

0
 
LVL 15

Author Comment

by:gplana
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 14

Assisted Solution

by:sandeep_patel
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.

Thanks,
Sandeep
0
 
LVL 15

Author Comment

by:gplana
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 ?
0
 
LVL 14

Accepted Solution

by:
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.

Thanks,
Sandeep
0
 
LVL 15

Author Comment

by:gplana
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.
Regards.
 

Window w



if i_am_on_project_1 then

     open(w,"window1")

else

    open(w,"window2")

end if

Open in new window

0
 
LVL 15

Author Closing Comment

by:gplana
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now