Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to access a local variable of one method into other method in same class in c# in windows application.

Is it possible??
Posted

Noup, local variables are only accessible from within their scope. They live on the stack and simply don't exist when "their" method is not executed at the moment.

You will have to promote your variable to member level to access it from different methods.
 
Share this answer
 
The closest thing that somehow relates to your question that I can think of are ref-Parameters in method calls:
C#
void SomeMethod()
{
    int x = 1;
    SomeOtherMethod(ref x);
    // at this point x==2
}

void SomeOtherMethod(ref int a)
{
    a = 2;
}

As you can see, ref-Parameters allow the called method to modify the value of a variable within the calling method's scope. But other methods that aren't called from SomeMethod() have no way to modify the value of x.

Heavy use of ref-Parameters is generally bad practice, you should think twice before using it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900