How to modify global variable in a function?

Hello,
I want to modify a globalvariable in a function, at first I did it this way:
class Global_output_class
GlobalDim("Correlation_Status,fail_part,End_Exp")
dim pouet
Correlation_Status = 12
Call Correlation()
pouet = Correlation_Status
Function Correlation()
Dim Global_output_class_sub
Set Global_output_class_sub = new Global_output_class
Correlation_Status = 1
fail_part = 2
End_Exp = 3
Global_output_class_sub.CorrelationStatus = Correlation_Status
Global_output_class_sub.failpart = fail_part
Global_output_class_sub.EndExp = End_Exp
set Correlation = Global_output_class_sub
End function
In this case: correlation_status receive the value 12, then I go to my function correlationn() where it became 1
Then it goes out of the subfunction and takes the previous value from the program(12) ( I dont want that)
To solve the problem I made it this way:
class Global_output_class
public CorrelationStatus
public failpart
public EndExp
end class
GlobalDim("Correlation_Status,fail_part,End_Exp")
Correlation_Status = 12
Set Global_Output = Correlation()
Correlation_Status = Global_Output.CorrelationStatus
fail_part = Global_Output.failpart
End_Exp = Global_Output.EndExp
pouet = Correlation_Status
Function Correlation()
Dim Global_output_class_sub
Set Global_output_class_sub = new Global_output_class
Correlation_Status = 1
fail_part = 2
End_Exp = 3
Global_output_class_sub.CorrelationStatus = Correlation_Status
Global_output_class_sub.failpart = fail_part
Global_output_class_sub.EndExp = End_Exp
set Correlation = Global_output_class_sub
End function
This way my global value are recopied in themselves after leaving the subprogram
I got a lot of variables, is there any easier way so the global variable modified in a function keep the value after leaving the function?
Thanks for help,
Fred
Solved!
Go to Solution.

Hi Fred,
it is possible to use a global defined variable but the better way is to use to use a funtion call (or procedure call) with parameters. Please find first the good solution for a funcion call with parameter and the sub-optimal way with an global valiable:
dim oParameter
set oParameter = new cGlobal_output_class
oParameter.Correlation_Status = 12
msgbox "Correlation_Status before Call Correlation: " & oParameter.Correlation_Status
Call Correlation(oParameter)
msgbox "Correlation_Status after Call Correlation: " & oParameter.Correlation_Status
Function Correlation(oPara)
msgbox "Correlation_Status in the FUNCTION before change: " & oPara.Correlation_Status
oPara.Correlation_Status = 1
oPara.fail_part = 2
oPara.End_Exp = 3
msgbox "Correlation_Status in the FUNCTION after change: " & oPara.Correlation_Status
End function
class cGlobal_output_class
dim Correlation_Status,fail_part,End_Exp
end class
call GlobalDim("oPouet")
dim oPouet
set oPouet = new cGlobal_output_class
oPouet.Correlation_Status = 12
msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status
Call Correlation()
msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status
Function Correlation()
msgbox "Correlation_Status in the FUNCTION before change: " & oPouet.Correlation_Status
oPouet.Correlation_Status = 1
oPouet.fail_part = 2
oPouet.End_Exp = 3
msgbox "Correlation_Status in the FUNCTION after change: " & oPouet.Correlation_Status
End function
class cGlobal_output_class
dim Correlation_Status,fail_part,End_Exp
end class
Greetings
Walter

Similar Messages

  • Using a global variable within a function

    Hi all,
    I'm trying to learn how to use global variables, but can't quite get it right:
    Im trying to use two buttons, named UpButton and DownButton to control one global variable, p1
    A text field, named "Value" displays p1.
    Here's my code:
    p1 = 0;
    ButtonClick("UpButton", p1, 100,"Value");
    ButtonClick("DownButton", p1,(-100),"Value");
    function ButtonClick(ButtonName,Var,Increment,TextToChange){
                        sym.$(ButtonName).click(function(){
                        Var = Var + Increment;
                        sym.$(TextToChange).html(Var);
    However, it seems to be treating p1 as two different variables.
    Here's a link to the page to show you what I mean, apologies but this link wont work in google chrome because I'm using dropbox as a server temporarily
    In other browsers you may need to click "Show all content" or similar to any security warnings...
    https://dl.dropboxusercontent.com/u/83693404/ButtonTest/buttons.html
    Here are the project files:
    https://app.box.com/s/0shocivto8fl295h62uq
    Any help greatly appreciated, sorry if this is a stupid question,
    Katherine

    Hi Katherine,
    However, it seems to be treating p1 as two different variables.
    Indeed, your code created two different variables in two different scopes (I guess it as to do with JavaScript closures, but do  not ask me more, I am still a bit foggy about that idiosyncrasy ).
    In JavaScript, a primitive parameter – this is the case here with p1, a Number value –, is passed by value. In other words, a copy of the value is made. Your code handles two copies of the value stored in p1.
    This contrasts with an object parameter, which is passed by reference (address in memory), without copy. Your code would work if Numbers were passed by reference : the two Var (p1) would then point to the same address.
    The intent behind your trial is a global variable.
    1) Let us be as simple as possible :
    Stage : document.compositionReady event handler
    p1 = 0;
    buttonClick = function( Increment)
      p1 += Increment;
      sym.$( "Value").html( p1);
    Stage : DownButton.click event handler
    buttonClick(-100);
    Stage : UpButton.click event handler
    buttonClick(100);
    Without being preceded by the var keyword the variables are global.
    So the Number variable p1 can be accessed inside function buttonClick.
    And the Function variable buttonClick as well, is accessible inside click event handlers, everything (function definition +function calls) being inside the same symbol (Stage).
    2) Now, suppose this is no longer the case : you want to access a variable from another symbol.
    We create a new GraySquare symbol, instantiated both in the DownButton symbol (instance name DownSquare) and in the UpButton symbol (instance name DownSquare).
    Stage : document.compositionReady event handler
    p1 = 0;
    sym.buttonClick = function( Increment)
      p1 += Increment;
      sym.$( "Value").html( p1);
    The sym. prefix is now necessary to make the variable (here a Function, but you would proceed exactly the same with a String, Number or Boolean) accessible in other symbols.
    DownButton symbol : DownSquare.click event handler
    sym.getComposition().getStage().buttonClick( -100);
    UpButton symbol : UpSquare.click event handler
    sym.getComposition().getStage().buttonClick( 100);
    From these other symbols, the sym.getComposition().getStage(). prefix is necessary to acces to our global variable (function).
    The two examples are downloadable here : https://app.box.com/s/6vkyiqk7i8zwlw0j1wk1
    Gil

  • Webdynpro - how to add global variables and common proj to existing proj

    How to add global variable in either ViewController or CustomController.  We realise that codes must be added within the begin and end exction.  Codes outside that will be deleted when saved. 
    How can we add a common WDP project to an existing project?  We have actually added a common wdp project at the project references screen.  But during runtime, we encouter error.  The error is classNotFoundException.  The class is the class created in the common project.

    Hi.
    I think you need to assign ProB to ProA.
    Step1.
    Open propety of ProjectA.
    Step2.
    Select WebDynproRefrences
    Step3.
    Select Sharing references
    Step4.
    Choose add button.
    Step5.
    If your projectA named "testapp" and you are not using
    DC "local/testapp" is the proper name.
    I hope that it work!!!.

  • How does a global variable affect the runtime of optimisation-function fminsearch?

    Hi,
    my function "fmintest" needs 254s ~ 4min to finish calculation in Mathscript, while Matlab needs
    3s!!! I need the sorfware for data processing in a real time measuring system, where the calculation have to be done within 5 seconds.
    In the Mathscript-help I found the following (global -> details):
    If you call this function from a MathScript Node, LabVIEW operates with slower
    run-time performance for the MathScript Node. To optimize the performance of the
    MathScript Node, remove this function from scripts.
    How can I remove needed global variables in the fminsearch function handle? Is the system faster if I would/can remove the global variables?
    How can I avoid Mathscript-breakdown after saving changes to m-files and restarting them again?
    regards
    Attachments:
    ni3.7z ‏2 KB

    Hi,
    can you provid a VI and not only your m-files? It will be easier to help you!
    Can the customer provide a VI that demonstrates this behavior, rather than just providing the m files?
    Also, the Help file says fminsearch is not supported by the
    LabVIEW Run Time engine, so please take that in mind. If you want to build an application you cant use this function.
    Regards, RMathews

  • Simple Question -- How to use global variables in an another function??!

    Function named globalLeftMargin that has:
    Global NumberVar LeftMargin := 5;
    HOW DO I USE THIS IN ANOTHER FUNCTION?  B/C IT DOES NOT RECOGNIZE IT AND GIVES THIS ERROR:
    The ) is missing.
    The other function is:
    Space(LeftMargin)

    Hi Ajay,
    Add WhilePrintingRecords at the beginning of each formula like:
    WhilePrintingRecords;
    Global NumberVar LeftMargin := 5;
    WhilePrintingRecords;
    Global NumberVar LeftMargin;
    Space(LeftMargin)
    Good luck,
    Brian

  • [Bash] How to `declare` a global variable inside a function?

    #!/bin/bash
    # Not using declare -A, array will be treated as indexed array rather than an associative array
    seta()
    for ((n=0;n<$1;n++)); do
    printf -v a_$n[k] %s k$n
    printf -v a_$n[j] %s j$n
    eval echo inside seta: a_$n[k] = \$\{a_$n\[k\]\}
    done
    seta 3
    echo outside: a_0[k] = ${a_0[k]}
    echo outside: a_0[j] = ${a_0[j]}
    echo
    # Use declare -A inside function, array will be undefined outside
    setb()
    for ((n=0;n<$1;n++)); do
    declare -A b_$n # Note here
    printf -v b_$n[k] %s k$n
    printf -v b_$n[j] %s j$n
    eval echo inside setb: b_$n[k] = \$\{b_$n\[k\]\}
    done
    setb 3
    echo outside: b_0[k] = ${b_0[k]}
    echo outside: b_0[j] = ${b_0[j]}
    echo
    # The bad solution, only works if we know beforehand how many c_? arrays will be assigned inside setc()
    for ((n=0;n<3;n++)); do
    declare -A c_$n
    done
    setc()
    for ((n=0;n<$1;n++)); do
    printf -v c_$n[k] %s k$n
    printf -v c_$n[j] %s j$n
    eval echo inside setc: c_$n[k] = \$\{c_$n\[k\]\}
    done
    setc 3
    echo outside: c_0[k] = ${c_0[k]}
    echo outside: c_0[j] = ${c_0[j]}
    My original code does the declare -A as in setb(). I was surprised when I saw all blank output... Now the problem is illustrated clearly by the setb() example.
    The setc() example works, but a bit ugly: look at the two 3's...
    My ideal solution would be, something like `declare_global -A b_$n` in setb()
    Any ideas?

    with current bash versions, i don't think it is possible to declare global associative arrays in functions.
    what you are after is the "-g" option of typeset which is available in zsh.
    i think i read somewhere that something similar may be planned for bash 4.2.

  • How to use session variable in JSP function  & How to use both JSP  Servlet

    Hi,
    I am new to JSP and servlets
    Still I am devloping a website in JSP. I am not mixing JSP with servlets, but I do create Java files for bean, logic and database works.
    I try to keep the hard coding part out of JSP.
    I dont how to use both JSP and Servlets in combination.
    Hence If needed I write some functions in JSP.
    but it gives me error
    +<%! public void abc()+
    +{+
    int intUserId = Integer.valueOf((Integer) session.getAttribute("MySession_UserID"));
    +}+
    +%>+
    Saying cannot find symbol session
    1) So can u please tell how can I access session variables within JSP function
    2) And also give me some links/tutorials about useing both JSP and Servlets in combination.
    Thanks
    Venkat

    The application architecture when you use Servlets and JSP in a standard MVC pattern is explained here (under the heading "Integrating Servlets and JSP Pages") and here ...

  • How to Decalre Global Variable in UDF

    Hi All,
    Can you please help me, how to declare a Global variable in UDF.
    I am using SAP PI 7.0.
    Regards,
    Manian.

    Hi manian,
    Have a look this thread:
    Global Variable - How to Set and Access
    Carlos

  • How to change Global variable in the query

    Hi,
    I have 6 reports which is using 0PDT as a global variable and has single option as a property. Now I want to change this property and have multiple selection but I am not able to change its property.
    I am able to see all the filter variables from the query property and from there I am getting edit option to change but the "single selection" option is grey/disable which is preventing me to make change in this variable. Is there any way that I delete this variable and create a local variable which has date range option.
    This is affecting all my reports and all the report needs date ranges instead of single select option.
    Thanks,
    Shivani

    Shivani,
        You can change the restrictions with new variable(right click on required infoobject and choose restrict -->> choose variables -->> move existing variable from right to left and move  new variable. Then you can see only one variable.
    You can not delete any variable if is using in some queries.
    how to restrict:
    Srini<a href="http://help.sap.com/saphelp_nw04/helpdata/en/f1/0a563fe09411d2acb90000e829fbfe/content.htm">Restricting Characteristics</a>

  • Change a global variable with a function

    HI ALL!
    I have the following problem...
    I would like to change a global variable with a void function so not like this: variable = function(); .
    What do you suggest?
    Best regards,
    Korcs

    I have the following problem...
    I would like to change a global variable with a void
    function so not like this: variable = function(); .
    What do you suggest?>
    >
    I dont quite understand your question but if your variable is a reference to an object then you can certainly have a method to alter the object that the reference is pointing to and/or the reference itself..
    otherwise your 'function' will have to return the same type as the variable itself in order to do the assignment as you demonstrated, regardless of wether it's C, C# or Java, AFAIK, WIRNM
    HTH

  • The global variable panel has a menu item under OPERATE called REINITIALI​ZE ALL TO DEFAULT. How can all global variables be reinitiali​zed at to their default values at program startup?

    Each time I restart my application, I would like all global variables to be reset to their default values without having to reset each one individually. Can I do this? If so, how?

    Sure you can do it. Enclosed is a VI which checks all VIs in memory and reinitializes any that are of type Global VI.
    Attachments:
    Reinit_Globals.vi ‏22 KB

  • How to print global variable in smart form table cell

    hi,
    i have smartform and i insert table in it, i also have set global variable, i want to show variable in table cell.
    regards
    shahid rais

    Hi Shahid Rais 
    You can use this global variable that has been declared in anywhere as you like.
    For displaying global variable in table lines, You can first set the column and row of the table by using the table tab in table node . Then, u can decide either the variable u want to used will be placed on header, main, or footer. After that, right click on one of the table part, and choose create table line. After choosing the correct line type as already been declared in the table painter, just simply right click on the table cell, and then create text node.
    You can now enter your variable by using "insert field".
    And don't forget the & & signs before and after your variable name.
    Regards,
    William Prawira

  • How to pass global variables to call stored procedure in form personalizati

    Hi,
    We want to call a custom store procedure with 2 paramterts, I am storing values into 2 global variables.
    We want call the custom store procedure with global variable values in form personalizations.
    We tried like
    ='declare
    begin
    SUR_TEST_ORDER_LINE_UPD.update_order_line (:global.xx_line_id, :global.ship_set_id);
    end'
    Could you please suggest.
    Advance Thanks
    Subbu

    Hi,
    Doc number (MOS Doc 743490.) is avaiable in metalink or ?Yes.
    Note: 743490.1 - Customization in Oracle Applications
    https://metalink2.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=743490.1
    Regards,
    Hussein

  • How to declare global variables using another global variable in ODI

    I am declaring a gloabal variable using another global variable.
    say for example:
    I have a global variable empid.
    I am decalaring another global variable empname in refreshing tab of global variables and the select statment is
    select empname from emp where empno = #GLOBAL.empid -------if i write like this i am getting error as invalid character.
    select empname from emp where empno = '#GLOBAL.empid'-------if i write like this i am getting error as invalid number.
    I have kept the datatype as numeric and action as non persistent
    Please help
    Thank you in advance.

    Hi,
    You cant test/refresh empname standalone.
    You need to create a new package drag and drop both variables and make them as refresh variable and execute that package and test.
    Flow,
    empid----> empname
    Thanks,
    Guru

  • How to find Unused variables in procedure,function or package

    Hi all,
    I want find out unused variables in procedure, function and package.
    I have written below script for doing this ,but i am not getting the expected result.
    Kindly help me to improve the below code ,so that it works as expected.
    {code}
    version details
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    "CORE    11.2.0.3.0    Production"
    TNS for Linux: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    {code}
    {code}
    What i Have tried is This.
    DECLARE
      V_OBJECT_NAME VARCHAR2(30) :='PR_PRINT';
      V_OBJECT_TYPE VARCHAR2(30) :='PROCEDURE';
      CURSOR C1(CP_OBJECT_NAME VARCHAR2,CP_OBJECT_TYPE VARCHAR2)
      IS
        SELECT US.NAME,
          US.TYPE,
          US.LINE,
          REGEXP_SUBSTR(US.TEXT,'.* ') AS var_name
        FROM user_source US
        WHERE name=CP_OBJECT_NAME
        AND type  =CP_OBJECT_TYPE
        AND REGEXP_LIKE (TEXT,'(v_|g_|c_)','i')
        AND REGEXP_LIKE (TEXT,'^[^ ]')
        AND REGEXP_LIKE (TEXT,'^[^--]') ;
      v_count NUMBER ;
    BEGIN
      FOR i IN C1(V_OBJECT_NAME,V_OBJECT_TYPE)
      LOOP
        SELECT COUNT( *)
        INTO V_COUNT
        FROM USER_SOURCE US
        WHERE US.NAME=I.NAME
        AND REGEXP_LIKE(US.TEXT,i.var_name,'i' )
        AND US.LINE<>I.LINE;
        IF V_COUNT  =0 THEN
          DBMS_OUTPUT.PUT_LINE('variable '||I.VAR_NAME||'Is declared at line#'||I.LINE||' But no where used');
        END IF ;
      END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('p_err_code := '||SQLCODE||dbms_utility.format_Error_backtrace());
      DBMS_OUTPUT.PUT_LINE('p_err_msg := '||sqlerrm);
    END ;
    {code}
    Thanks,
    P Prakash

    Hello,
    as suggested by padders you can use PL/Scope, an example:
    ALTER SESSION SET PLSCOPE_SETTINGS='IDENTIFIERS:ALL';
    CREATE OR REPLACE PACKAGE ui_test1 AS
        global_number   NUMBER := 9;
    FUNCTION get_number
        RETURN NUMBER;
    END ui_test1;
    CREATE OR REPLACE PACKAGE BODY ui_test1 AS
    PROCEDURE setNull
         p_varchar          IN OUT VARCHAR2
    IS
    BEGIN
        p_varchar := NVL(p_varchar,'NULL');
    END setNull;
    FUNCTION get_number
        RETURN NUMBER
    IS
        FUNCTION setZero
             p_number       IN NUMBER
            RETURN NUMBER
        IS
        BEGIN
            RETURN NVL(p_number,0);
        END setZero;
    BEGIN
        RETURN global_number;
    END get_number;
    END ui_test1;
    SELECT  DISTINCT
            object_name
           ,object_type
           ,name
           ,type
           ,line
           ,col
    FROM    all_identifiers obj
    WHERE   obj.owner = USER
    AND     obj.usage = 'DECLARATION'
    AND     obj.object_name = 'UI_TEST1'
    AND     NOT EXISTS (
                SELECT  1
                FROM    all_identifiers with_rh
                WHERE   obj.signature = with_rh.signature
                AND     with_rh.usage IN ('REFERENCE','CALL','ASSIGNMENT')
    ORDER BY TYPE
            ,object_name
            ,object_type
            ,name;
    OBJECT_NAME  OBJECT_TYPE   NAME       TYPE       LINE COL
    UI_TEST1     PACKAGE       GET_NUMBER FUNCTION     11  10
    UI_TEST1     PACKAGE BODY  SETZERO    FUNCTION     35  14
    UI_TEST1     PACKAGE       UI_TEST1   PACKAGE       1   9
    UI_TEST1     PACKAGE BODY  SETNULL    PROCEDURE    12  11
    Regards
    Marcus

Maybe you are looking for

  • Godaddy email send problem

    I don't know if anyone else has had this problem but my godaddy email account cannot send emails from my iPad. I set it up the same way as my iphone and it think it worked initially but now, I've tried so many different variations of smtpout.securese

  • How can i select the board gain of a Lab-PC+ ?

    Hi, I need to use a DAQ Board Lab-PC+ to acquire an analog signal. It varies from -1 to 1 V, and i´d like to use all the range of the AD converter. The Lab-PC+ manual indicates a software selection board gain, but i don´t know how to do it, or which

  • SQLE_COMMUNICATIONS_ERROR while running SFA application from Visual Studio

    Hi, I am trying the SFA (Sales Force Automation) example. I am using the Windows Mobile 6.0 emulator from Visual Studio to run the application. When I go to the "Configure" button, change the configuration values and try to Save, I am getting the fol

  • XPATH + namespaces in 8i not possible?

    Hi, im trying to use the XPATH helper package to search a XML document that uses namespaces but it cant match anything: <bt1:root xmlns:bt1="http://127.0.0.1"> <bt1:Error> <test>test value</test> for example i want 'test' yet when i try to get it usi

  • Suitable Apple Extention  for a PC video

    I own a CD Rom of a video that can only be opened by WIndows 95 or higher, PC format only. It is part of a course on vocal improvement. A friend has a PC, and thinks he can copy the video for me, but he is not current with Apple requirements, and has