enum type for color

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
How do i create an enum type for color ? Something like this

VB.NET:
Expand Collapse Copy
Enum colorType
   CompanyA = Color.Red
   CompanyB = Color.Blue
End Enum

Since it only accept constant this won't work. Right now i am just using a function with a select case to translate the enum type to the color. But how do you do it directly?
 
There are a couple of ways to do what you want, but none of them involve using an enum in the manner you are asking about. Enums are by definition an integer type, so while you can't assign a System.Drawing.Color structure to an enum value, you can certainly assign the integer value and use that as an enum, but that will require you to convert it to a color before using it. This is also kind or difficult to manage because you must know what the value is for the color so you can assign it. Through reflection you can extract all of the color values. As was stated earlier all of the colors are essentially enums anyway that are modified by the color struct to be able to be used by the objects that require a color structure.

Example:
VB.NET:
Expand Collapse Copy
'This is the KnownColor value for system colors
Public Enum colorType
    companyA = &H8D
    companyB = &H25
End Enum

To use this as a color you would have to explicitly coerce it from an integer to a color.

If you simply want to alias colors for a company, why not just use read only variables?

VB.NET:
Expand Collapse Copy
Public ReadOnly CompanyA As Color = Color.Red
Public ReadOnly CompanyB As Color = Color.Blue

It sure as heck beats trying to manipulate the underlying structure to do something that it wasn't designed to do.
 
Back
Top