473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I associate a value with an enum type?


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
5 3844
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


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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
6
by: Ashok | last post by:
hi, i want to know how to make a specific type of file open in an application i developed in python when the user clicks on the file.(in windows) for eg. a .txt file when clicked opens in notepad,...
2
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
4
by: Chris Bower | last post by:
Reposted from aspnet.buildingcontrols: Ok, I've got a bunch of derived controls that all have a property Rights of type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so that I...
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
7
by: John Goche | last post by:
Hello, The following program compiler and runs fine under gcc and produces the output 3. However, I am not sure whether such behavior is legal. In particular, as a related question, how would I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.