none
How to call variable from another function using csharp ? RRS feed

  • Question

  • problem

    How to call variable from another function using csharp ?

    public int function1()

    {

    int a=5;

    }

    on function2 i need to get value of a that equal 5

    public int function2()

    {

    int b = function1.a that have =5;

    }

    are this possible call variable from another function on csharp ?

    How

    Saturday, September 7, 2019 5:03 AM

Answers

  • int b = function1.a

    No, you cannot do that, and it is a GOOD thing that it cannot be done.

    In C# an similar languages, there is a concept which is the scope of a variable. This means that a variable can be declared in various ways and in various places of the code, and this declaration determines from which places the variable can be reached. This is useful because reducing the visibility of a variable makes the code more robust because you can be assured that it won't be modified from an unexpected place, and its value won't be read from where you don't expect it to be read.

    Specifically, a variable declared inside a method has a scope that only lets it be accessed from within the method, and not from outside. This isolates the internals of the method from the rest of the program, so that you can modify the internal implementation of the method whenever you need to and be assured that the internal changes will not break anything else in other parts of the program.

    Therefore, the C# compiler explicitly forbids you from doing what you are trying to do. Or, conversely, you are telling the compiler when you decide to declare a variable inside a method "hey, compiler, I won't ever be accessing this variable from anywhere else, so give me an error if I mistakenly try to do it".

    To summarize, if you want to access the variable, you should either return it as a result from the method, or declare it with a different scope. For instance, you could declare it at class level if you want to access it from other methods in the same class.

    Edit: Note that, in addition to the preceding, there is another consequence of declaring the variable with a method scope: Since it is impossible to access the variable from anywhere else except the method, it is not necessary to preserve the value of the variable once execution has exited from the method. In practice, this is achieved by storing the variable in the Stack. As soon as the method exits, the Stack is "popped" and all the values saved there disappear. So, even if the compiler would let you access the variable like you want to, it wouldn't make any sense because the variable would already have disappeared by the time you tried to access it.
    Sunday, September 8, 2019 3:34 PM
    Moderator

All replies

  • Hello engahmedbarbary,

    1. Generally speaking, this is not possible because local variables exist only on the function stack and will not be accessible once the method ends.

    2. Is your objective to somehow "remember" what was set in "a" by function1() ?

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/


    • Edited by Lim Bio Liong Saturday, September 7, 2019 9:14 AM
    • Proposed as answer by D Avik Saturday, September 7, 2019 9:27 PM
    Saturday, September 7, 2019 7:03 AM
  • sry i was still sleeping that morning.. this works for me

    //Function1

            private void VarSend()
            {
                int VarA = 5;
                string VarAS = VarA.ToString();

                ImportVar(VarAS);
            }

    //Function2

            private void ImportVar(string VarAS)
            {
                int parsedNumber = int.Parse(VarAS);
                int UsableNumber = parsedNumber;
            }

    • Proposed as answer by Inspectah84 Sunday, September 8, 2019 11:51 AM
    Sunday, September 8, 2019 11:12 AM
  • Hi,

    No, it's not possible.

    Variables are only visible and accessible within the curly brackets where they are declared. If you want that variable 'a' is accessible from function2(), you should move 'a' to a higher level within the nested bracketing.

    class MyClass
    {
       int a = 5;
    
        void func1()
        {
            int b = a; // Ok
    
            {
                int c = a; // Ok
            }
    
            int d = c; // 'c' doesn't exist anymore here.
        }
    }

    Best Regards,

    Heiko

    Sunday, September 8, 2019 3:29 PM
  • int b = function1.a

    No, you cannot do that, and it is a GOOD thing that it cannot be done.

    In C# an similar languages, there is a concept which is the scope of a variable. This means that a variable can be declared in various ways and in various places of the code, and this declaration determines from which places the variable can be reached. This is useful because reducing the visibility of a variable makes the code more robust because you can be assured that it won't be modified from an unexpected place, and its value won't be read from where you don't expect it to be read.

    Specifically, a variable declared inside a method has a scope that only lets it be accessed from within the method, and not from outside. This isolates the internals of the method from the rest of the program, so that you can modify the internal implementation of the method whenever you need to and be assured that the internal changes will not break anything else in other parts of the program.

    Therefore, the C# compiler explicitly forbids you from doing what you are trying to do. Or, conversely, you are telling the compiler when you decide to declare a variable inside a method "hey, compiler, I won't ever be accessing this variable from anywhere else, so give me an error if I mistakenly try to do it".

    To summarize, if you want to access the variable, you should either return it as a result from the method, or declare it with a different scope. For instance, you could declare it at class level if you want to access it from other methods in the same class.

    Edit: Note that, in addition to the preceding, there is another consequence of declaring the variable with a method scope: Since it is impossible to access the variable from anywhere else except the method, it is not necessary to preserve the value of the variable once execution has exited from the method. In practice, this is achieved by storing the variable in the Stack. As soon as the method exits, the Stack is "popped" and all the values saved there disappear. So, even if the compiler would let you access the variable like you want to, it wouldn't make any sense because the variable would already have disappeared by the time you tried to access it.
    Sunday, September 8, 2019 3:34 PM
    Moderator