Reputation: 998
I have a C# console app. I'd like the main program to determine if I'm in DEBUG mode, for example, and set a variable, say... g_Bypass which every class can just refer to.
Example:
class test
{
public static bool g_testMode;
test()
{
g_testMode = true; // read from the database.
// more code...
Object1 _obj = new Object1();
// do more stuff with _obj...
}
}
class Object1
{
public Object1()
{
// constructor
if (g_testMode) // << I'd like to just refer to it this way!
{
// do something
}
}
}
Upvotes: 2
Views: 109
Reputation: 633
If it is debug mode you are worried about you could create a local variable in a method or a member variable within a class definition by doing the following:
#if DEBUG
bool debug = true;
#else
bool debug = false;
#endif
If for some reason you want to create a global static:
public static class Test {
#if DEBUG
public static bool debug = true;
#else
public static bool debug = false;
#endif
}
This needs to be referenced outside of the object as Test.debug
You need to define the variable DEBUG
in the project's build screen
JUST BECAUSE YOU CAN DO THIS DOESN'T MEAN YOU SHOULD!!!
Upvotes: 1
Reputation: 2130
No. In .Net every variable and method has to be enclosed in a class. However, a static public member is accessible with global scope, but you have to prefix it with the class name when using it outside of the class. But you could call your class g
, than instead of g_testMode
you have to write g.testMode
. Although lower-case class names are discouraged, maybe better G.testMode
.
Your example would than read as:
static class G //static is not necessary here
{
public static bool testMode;
}
class test
{
test()
{
G.testMode = true; // read from the database.
// more code...
Object1 _obj = new Object1();
// do more stuff with _obj...
}
}
class Object1
{
public Object1()
{
// constructor
if (G.testMode) // << I'd like to just refer to it this way!
{
// do something
}
}
}
Upvotes: 2