global variable in console applications
give me an example for global variable in console applications?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 14, 2013, 4:49 AM
If you're going to do that, then you might as well make them static so that there's only the one copy anyway.
Iftikar HussainPosted Jul 14, 2013, 4:36 AM
Here it is
Since by default for every console application Main() method is entry point and it is a static. In static method you can use only static variable only
Regards,
Iftikar
Sreekanth ReddyPosted Jul 14, 2013, 4:22 AM
VulpesPosted Jul 14, 2013, 4:02 AM
All variables must be defined within a class or struct.
However, one way to simulate a global variable is to add an internal static field to the class which contains your Main() method. For example:
class Program
{
internal static int GlobalInt = 42;
static void Main()
{
// blah
}
}
You can now access the global variable from any other class in the application using the expression Program.GlobalInt.