locked
Using variables in differents private void button_click event RRS feed

  • Question

  • So for the purpose of learning I've tried to create a new button and the event triggered by the click of it makes the sum of two variables :

    private void button_sum_Click(object sender, EventArgs e)
            {
                 int h = 5;
                 int z = 7;
    
                 int sum = h + z; 
            }

    The result and everything is fine, but I tried to create another button that should decrease by one the result of that addiction,

    so that upon pressing it, the variable sum is decreased by one :

    private void minus_sum_Click(object sender, EventArgs e)
            {
               int sum = sum - 1; 
            }

    I know this has something to do with private/public/abstract/static void but would someone please explain me how to access that variable without having to create a new class ?

    Sunday, December 22, 2013 2:34 AM

Answers

  • I know this has something to do with private/public/abstract/static void but would someone please explain me how to access that variable without having to create a new class ?

    It doesn't have anything do with any of those things.  It is purely a question of scope.  The scope of a variable is the block that it's declared in, i.e. a variable only exists within that block.

    Because you are declaring your variables inside your methods, each variable only exists within the method it's declared in.  You could reduce the scope even further by declaring them inside a loop or an 'if' block.  The fact that they have the same name does not make them the same variable.

    By following jdweng's advice, you enlarge the scope of the variable to the class, so you can then access the variable anywhere within the class, which means you can access the same variable in multiple methods.

    • Marked as answer by Rphysx Sunday, December 22, 2013 3:23 PM
    Sunday, December 22, 2013 3:29 AM

All replies

  • Declare the variable in global memory space by placing the declaration outside of both functions.  Remove "int" from 2nd function as well.

            int sum;        
            private void minus_sum_Click(object sender, EventArgs e)
            {
               sum = sum - 1; 
            }


    jdweng

    Sunday, December 22, 2013 2:48 AM
  • I know this has something to do with private/public/abstract/static void but would someone please explain me how to access that variable without having to create a new class ?

    It doesn't have anything do with any of those things.  It is purely a question of scope.  The scope of a variable is the block that it's declared in, i.e. a variable only exists within that block.

    Because you are declaring your variables inside your methods, each variable only exists within the method it's declared in.  You could reduce the scope even further by declaring them inside a loop or an 'if' block.  The fact that they have the same name does not make them the same variable.

    By following jdweng's advice, you enlarge the scope of the variable to the class, so you can then access the variable anywhere within the class, which means you can access the same variable in multiple methods.

    • Marked as answer by Rphysx Sunday, December 22, 2013 3:23 PM
    Sunday, December 22, 2013 3:29 AM