How can I declare the Color
type as const
like this:
private const Color MyLovedColor = Color.Blue;
That doesn't work because the Color.Blue is static not const.
(readonly won't help me because I need the color for an attribute which "support" constants only
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);
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