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.

Was it helpful?

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.

http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top