0

I had declared a variable in a method. how can I call the variable in the main method?

31st Oct 2016, 8:46 AM
Fikri Buih
Fikri Buih - avatar
5 Antworten
+ 4
A variable declared within a method can only be accessed within that method and nowhere else. The scope of the variable cannot go beyond the method its declared in.
31st Oct 2016, 8:49 AM
Ousmane Diaw
+ 2
You can't call it in the main if you declare it in a method, you could declare it in the main and use it in the method (if you have to modify the variable you have to use it as a reference or it won't change after finishing the method ). Hope it helps
12th Nov 2016, 2:14 AM
C.E.
C.E. - avatar
0
the best think to do is to declare the variable private in the class and then you could just get or set the value in the whole class where you want
31st Oct 2016, 9:13 AM
StoneSmasher
StoneSmasher - avatar
0
For example: class Example { private static string variable; static void main (string [] args){ yourMethod(); Console.WriteLine (variable); } private static void yourMethod() { variable = "test"; } }
31st Oct 2016, 9:18 AM
StoneSmasher
StoneSmasher - avatar
0
owh. thanksss
31st Oct 2016, 9:32 AM
Fikri Buih
Fikri Buih - avatar