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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top