I have a below class:
public static class AllAcess
{
public static int var1;
//Some conditional statements
if(somecondition)
{
var1 = x;
}
else
{
var1 = y;
}
}
How can i put some conditional statement inside class. Currently it's not allowing if ,else etc .
Please suggest logic which can be used as condition here.
I want to access var1 from other classes.
You probably want a static constructor:
public static class AllAcess
{
public static int var1;
static AllAcess()
{
if (somecondition)
{
var1 = x;
}
else
{
var1 = y;
}
}
}
This is run at some point before var1 is accessed for the first time.
Note, do not do anything too complicated in a static constructor. Do not do anything which touches the filesystem or network, or does any threading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With