quick syntax question...

  • Thread starter Thread starter craig
  • Start date Start date
C

craig

Using C#, what is the best way to store a System.Drawing.Color value as a
constant?

Even though the Color struct is a value type, the following code does not
work:

public const System.Drawing.Color color = System.Drawing.Color.Blue;

Apparently, the Color struct cannot be evaluated at compile time. Can it be
stored as hex value?

Thanks!
 
You would have to use the readonly keyword, like this:

public readonly System.Drawing.Color color = System.Drawing.Color.Blue;

Because you can not execute code while declaring consts, they have to
have literals that can express them.

Hope this helps.
 
The const keyword can only be applied to primitive types (bool, char,
int, and so on).

You could use the readonly construct instead:

public readonly System.Drawing.Color color = System.Drawing.Color.Blue;

Regards,
Joakim
 
Thanks!!

Joakim Karlsson said:
The const keyword can only be applied to primitive types (bool, char, int,
and so on).

You could use the readonly construct instead:

public readonly System.Drawing.Color color = System.Drawing.Color.Blue;

Regards,
Joakim
 
Back
Top