Declare Color as constant

System.Drawing.Color is a struct, which means you cannot have a constant value of it.


Look at the KnownColor enumeration. It will likely cater for what you need.


You can assign a const only a value that is a literal. In your case I would then prefer a string literal and define your color as following:

const string mycolor = "Blue";

Then, wherever you need your color, you perform the backward conversion:

Color mynewcolor = Color.FromName(mycolor);

I am sorry, but this is the only way to keep it const.

EDIT: Alternatively you can also keep your color as (A)RGB attributes, stored into a single int value. Note, that you can use a hexadecimal literal then to explicitly set the different components of your color (in ARGB sequence):

const int mycolor = 0x00FFFFFF;
Color mynewcolor = Color.FromArgb(mycolor);

Tags:

C#

.Net