Constant instance in a static class
Question
I am tying to make a Global variable class, here is my code
public static class GLOBALVAR
{
public static const Color DIFFRENCECOLOR = System.Drawing.Color.LightSalmon;
public static const Color NOMATCHCOLOR = System.Drawing.Color.LightBlue;
}
but this does not want to work and I get this error
The type 'System.Drawing.Color' cannot be declared const
Is there a way to make this work.
Solution
you can use readonly
instead
public static readonly Color DIFFRENCECOLOR = System.Drawing.Color.LightSalmon;
the readonly
keyword means that your variable, DIFFRENCECOLOR
can only be modified in the constructor of it's class, GLOBALVAR
.
It's normally used for when you want to declare a "constant" at runtime, but it works for this purpose too.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow