PB Variables scope

Posted on 2006-05-22
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
Question by:mukkam_gafoor
    LVL 6

    Accepted Solution

    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:


    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 : = 2

    Author Comment

    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?

    LVL 6

    Expert Comment

    No special syntax to reference a shared variable, simply the variable name.
    Above precedence rules would be applied in case of duplicated name.

    Author Comment


    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
    LVL 6

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    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 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 use and create new code templates in NetBeans IDE 8.0 for Windows.

    760 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

    9 Experts available now in Live!

    Get 1:1 Help Now