By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,550 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

How can I associate a value with an enum type?

P: n/a

I am sure this is a really basic question but I don't see an easy way to do it.

I have an enum

public enum ColorType
{
red = 0,
green = 1,
blue = 2,
hue = 3,
saturation = 4,
brightness = 5
}

How can I associate a fixed value with each enum type? E.g. red is 255, hue=360 etc. Well, actually 255/fixedValue,
360/fixedValue etc.

I realize I could put these in a method with a switch statement and return the value but since the values are static or
at least only need to be calculated once at when the class is initialized it seems a waste of resources when I could end
up doing the calculation many times.

Is there a recomended way of doing something like this or at least a good way that doesn't involve lots of recasting of
the enum to ints.

Thanks,

Steve
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I'm confused by our question. Should ColorType.Red hold a 0
or 255? It could hold either.

Recasting of enum to int is very fast...

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"steve bull" <bu****@comcast.net> wrote in message
news:r4********************************@4ax.com...

I am sure this is a really basic question but I don't see an easy way to
do it.

I have an enum

public enum ColorType
{
red = 0,
green = 1,
blue = 2,
hue = 3,
saturation = 4,
brightness = 5
}

How can I associate a fixed value with each enum type? E.g. red is 255,
hue=360 etc. Well, actually 255/fixedValue,
360/fixedValue etc.

I realize I could put these in a method with a switch statement and return
the value but since the values are static or
at least only need to be calculated once at when the class is initialized
it seems a waste of resources when I could end
up doing the calculation many times.

Is there a recomended way of doing something like this or at least a good
way that doesn't involve lots of recasting of
the enum to ints.

Thanks,

Steve

Nov 17 '05 #2

P: n/a


I am using the enum within a widget to control the color. The enum determines which color type the widget controls.

If I used 255 etc then Red, Green and Blue would all have the same associated value - 255. I could not index off that
and know which color type I am dealing with. In fact there is no reason I couldn't have multiple values associated with
red.
Steve


On Fri, 6 May 2005 16:19:04 -0400, "Robbe Morris [C# MVP]" <in**@turnkeytools.com> wrote:
I'm confused by our question. Should ColorType.Red hold a 0
or 255? It could hold either.

Recasting of enum to int is very fast...


Nov 17 '05 #3

P: n/a
you should use a composite way.
For example
enum firstEnum : byte
{
a=0x0,
b=0x1,
c=0x2
.....
}
enum secondEnum: short
{
a=0x100,
b=0x101,
c=0x102,
....
}

enum compositeEnum : int
{
aa = firstEnum.a | secondEnum.a
}

or something similar. I think there is no need to define a compositeEnum
just use bitwise operations in your code..

--

Thanks,
Yunus Emre ALP÷ZEN
BSc, MCAD.NET

"steve bull" <bu****@comcast.net> wrote in message
news:ee********************************@4ax.com...


I am using the enum within a widget to control the color. The enum
determines which color type the widget controls.

If I used 255 etc then Red, Green and Blue would all have the same
associated value - 255. I could not index off that
and know which color type I am dealing with. In fact there is no reason I
couldn't have multiple values associated with
red.
Steve


On Fri, 6 May 2005 16:19:04 -0400, "Robbe Morris [C# MVP]"
<in**@turnkeytools.com> wrote:
I'm confused by our question. Should ColorType.Red hold a 0
or 255? It could hold either.

Recasting of enum to int is very fast...

Nov 17 '05 #4

P: n/a
Althought I don't really understand why you do it that way, you can solve
your problem with an array provided your color enum is sequential.

public enum ColorType
{
red = 0,
green = 1,
blue = 2,
hue = 3,
saturation = 4,
brightness = 5
}

ColorType SelectedColor = ColorType.red
int [] ColorMap = {255, 360 ,111, 322, 584, 641} // any value required
int MapedRed = ColorMap[(int)SelectedColor] ;

/LM

"steve bull" <bu****@comcast.net> wrote in message
news:ee********************************@4ax.com...


I am using the enum within a widget to control the color. The enum
determines which color type the widget controls.

If I used 255 etc then Red, Green and Blue would all have the same
associated value - 255. I could not index off that
and know which color type I am dealing with. In fact there is no reason I
couldn't have multiple values associated with
red.
Steve


On Fri, 6 May 2005 16:19:04 -0400, "Robbe Morris [C# MVP]"
<in**@turnkeytools.com> wrote:
I'm confused by our question. Should ColorType.Red hold a 0
or 255? It could hold either.

Recasting of enum to int is very fast...

Nov 17 '05 #5

P: n/a

thank you. it works fine for me.

steve

On Sat, 7 May 2005 06:22:34 +0200, "Luc E. Mistiaen" <lu**********@advalvas.be.no.spam> wrote:
Althought I don't really understand why you do it that way, you can solve
your problem with an array provided your color enum is sequential.

public enum ColorType
{
red = 0,
green = 1,
blue = 2,
hue = 3,
saturation = 4,
brightness = 5
}

ColorType SelectedColor = ColorType.red
int [] ColorMap = {255, 360 ,111, 322, 584, 641} // any value required
int MapedRed = ColorMap[(int)SelectedColor] ;

/LM

"steve bull" <bu****@comcast.net> wrote in message
news:ee********************************@4ax.com.. .


I am using the enum within a widget to control the color. The enum
determines which color type the widget controls.

If I used 255 etc then Red, Green and Blue would all have the same
associated value - 255. I could not index off that
and know which color type I am dealing with. In fact there is no reason I
couldn't have multiple values associated with
red.
Steve


On Fri, 6 May 2005 16:19:04 -0400, "Robbe Morris [C# MVP]"
<in**@turnkeytools.com> wrote:
I'm confused by our question. Should ColorType.Red hold a 0
or 255? It could hold either.

Recasting of enum to int is very fast...


Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.