Reputation: 150253
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
Upvotes: 12
Views: 15302
Reputation: 1
What if you used the long value returned by the RGB function as your constant?
for example the value returned for a light blue RGB(51,255,255)
is 16777011
so
Private Const ltBlue As Long = 16777011
txtbox1.backcolor = ltBlue
Upvotes: 0
Reputation: 12534
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);
Upvotes: 6
Reputation: 117240
Look at the KnownColor
enumeration. It will likely cater for what you need.
Upvotes: 12
Reputation: 6425
private static readonly Color MyLovedColor = Color.Blue;
I thanks that's the closest you can get?
Upvotes: 3
Reputation: 437376
System.Drawing.Color
is a struct
, which means you cannot have a constant value of it.
Upvotes: 4