Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PB Variables scope

Posted on 2006-05-22
5
Medium Priority
?
1,926 Views
Last Modified: 2013-12-26
I have a situation that is a variable with same name declared as local, instance, shared and global and in my button click i want to use the global and instance variables, where the local variable also exists. how it is possible?. normally any reference to the variable will affect the local only. this was an interview question and i can't answer.

pls let me know how it is possible in PB?

Thanks in advance
mukkam
0
Comment
Question by:mukkam_gafoor
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
tr1l0b1t earned 500 total points
ID: 16740877
From PB Help :

When PowerBuilder executes a script and finds an unqualified reference to a variable, it searches for the variable in the following order:

1      A local variable
2      A shared variable
3      A global variable
4      An instance variable

As soon as PowerBuilder finds a variable with the specified name, it uses the variable's value.

To refer to a global variable, you specify its name in a script. However, if the global variable has the same name as a local or shared variable, the local or shared variable will be found first.

To refer to a global variable that is masked by a local or shared variable of the same name, use the global scope operator (::) before the name:

::globalname

For example, this statement compares the value of local and global variables, both named total:

IF total < ::total THEN ...

To refer to instance variable where local variable with same name exists you need to qualify the name of the instance variable with the name of the object using dot notation :

w_test.total = 2
0
 

Author Comment

by:mukkam_gafoor
ID: 16748997
Hi tr1l0b1t

Thank you very much for this information and i will surely grade you A, just one more query what about shared variables? how will i refer it?

0
 
LVL 6

Expert Comment

by:tr1l0b1t
ID: 16762260
No special syntax to reference a shared variable, simply the variable name.
Above precedence rules would be applied in case of duplicated name.
0
 

Author Comment

by:mukkam_gafoor
ID: 16767029

I have graded you. But my question is like thsi,

Suppose i have local,shared and instance variable with say name 's_var'

and in the button click i can refer the local variabe by simply

s_var = 10 and instance variable w_win.s_var and here how will refer shared variable???

if there anyway to diffrentiate this, please let me know
0
 
LVL 6

Expert Comment

by:tr1l0b1t
ID: 16768627
Summary ...

Global variable :
::testvar = 1

Intance variable :
this.testvar = 1

Local variable :
testvar = 1

Shared variable :
testvar = 1

In a script with a local variable called 'testvar', you cannot access shared variable 'testvar'
since local variable will take precedence, so you should rename local variable if you want
to access both in the same script.

Naming conventions make scripts easy to understand and help you avoid name conflicts. A typical approach is to include a prefix that identifies the scope and the data type of the variable. For example, a prefix for an instance variable's name typically begins with i (such as ii_count or is_empname), a local integer variable's name would be li_total and a global integer variable's name would be gi_total.
(PB Help)

Let me know if still no clear.
Regards
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

578 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