473,385 Members | 1,661 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.

using flag attribute on an enum

I created this enumeration:

[Flags]
enum Status
{
Off = 0,
Red = 1,
Yellow = 2,
Blue = 4,
Overload = 8
}

(Not sure if the values are correct, or if I should use hex, or some
other numbers).

I have an array that contains either values of Off or Red. When I
compare values in the array, I'd like two Reds to be Yellow, and three
Reds (or Yellow and Read) to be Blue. Four Reds are Overload. How do I
do this, given my current array? Or is there a different way that the
array needs to be created for it to be 'aware' of the other colors?
Right now it just has Off and Red in it.
Nov 20 '05 #1
4 6049
Hello John

...not quite sure that this is what you want... anyways here goes:

public static string getMappedValue( Status[] statusArray )
{
int total=0;
foreach( Status s in statusArray )
total += (int) s ;

string result="";
foreach( Status s in System.Enum.GetValues( typeof(Status) ) )
if( total >= (int)s )
result=s.ToString();

return result;
}

This will give you the name of the calculated value, i.e 2 Reds will
give Yellow, 2 Yellow and a Blue gives you Overload. etc.
In this way you can control the values, and theire boundaries from
within the enum.

Nov 20 '05 #2

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:3p********************@rcn.net...
I created this enumeration:

[Flags]
enum Status
{
Off = 0,
Red = 1,
Yellow = 2,
Blue = 4,
Overload = 8
}

(Not sure if the values are correct, or if I should use hex, or some other
numbers).

I have an array that contains either values of Off or Red. When I compare
values in the array, I'd like two Reds to be Yellow, and three Reds (or
Yellow and Read) to be Blue. Four Reds are Overload. How do I do this,
given my current array? Or is there a different way that the array needs
to be created for it to be 'aware' of the other colors? Right now it just
has Off and Red in it.


You don't need flags here, just summing your array elements will do the
trick. It is a bit of work since you'll have to cast them to their base
type, but that is how I'd do it.

Also, change your enum to

enum Status
{
Red=1,
Yellow=2,
Blue=3,
Overload=4
}

That is, of course, unless you have another reason for flags
Nov 20 '05 #3
Daniel O'Connell [C# MVP] wrote:
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:3p********************@rcn.net...
I created this enumeration:

[Flags]
enum Status
{
Off = 0,
Red = 1,
Yellow = 2,
Blue = 4,
Overload = 8
}

(Not sure if the values are correct, or if I should use hex, or some other
numbers).

I have an array that contains either values of Off or Red. When I compare
values in the array, I'd like two Reds to be Yellow, and three Reds (or
Yellow and Read) to be Blue. Four Reds are Overload. How do I do this,
given my current array? Or is there a different way that the array needs
to be created for it to be 'aware' of the other colors? Right now it just
has Off and Red in it.

You don't need flags here, just summing your array elements will do the
trick. It is a bit of work since you'll have to cast them to their base
type, but that is how I'd do it.

Also, change your enum to

enum Status
{
Red=1,
Yellow=2,
Blue=3,
Overload=4
}

That is, of course, unless you have another reason for flags


Actually no, I don't. I considered just adding them up this way at
first, but I wasn't sure it would work. So instead of using the |
operator, I just use + to add them?

Is there a way to do this same thing, without an enum? I have an int
array of 0s and 1s (for off and red), and I could add those up, but then
where would the values of yellow, blue, and overloaded, be stored?
Nov 21 '05 #4

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:Us********************@rcn.net...
Daniel O'Connell [C# MVP] wrote:
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:3p********************@rcn.net...
I created this enumeration:

[Flags]
enum Status
{
Off = 0,
Red = 1,
Yellow = 2,
Blue = 4,
Overload = 8
}

(Not sure if the values are correct, or if I should use hex, or some
other numbers).

I have an array that contains either values of Off or Red. When I compare
values in the array, I'd like two Reds to be Yellow, and three Reds (or
Yellow and Read) to be Blue. Four Reds are Overload. How do I do this,
given my current array? Or is there a different way that the array needs
to be created for it to be 'aware' of the other colors? Right now it just
has Off and Red in it.

You don't need flags here, just summing your array elements will do the
trick. It is a bit of work since you'll have to cast them to their base
type, but that is how I'd do it.

Also, change your enum to

enum Status
{
Red=1,
Yellow=2,
Blue=3,
Overload=4
}

That is, of course, unless you have another reason for flags


Actually no, I don't. I considered just adding them up this way at first,
but I wasn't sure it would work. So instead of using the | operator, I
just use + to add them?


Well, you'll have to do int casts:

Status s = (Status)((int)(Status.Red) + (int)(Status.Blue));

Its ugly but it can be moved down to a utility method to save you from
having to look at it much.

Using | won't work here, since Status.Red | Status.Red == Status.Red, not
what you seem to want.
Is there a way to do this same thing, without an enum? I have an int array
of 0s and 1s (for off and red), and I could add those up, but then where
would the values of yellow, blue, and overloaded, be stored?


You certainly could, just using the integer values 0,1,2,3,4 in an array
that you could sum up quickly(and look up the number in an array to
determine the string or action to take.) Or you could write objects that
worked that way:

public class Status
{
....
}

public class Red : Status
{
....
}

and so on, using operator overloading to provide the use of + to sum items
and == to compare.
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Woon Kiat | last post by:
Hi, Using IDL, I can declare my enumeration like following, library MyAppLib { importlib("stdole32.tlb"); importlib("stdole2.tlb"); typedef enum MyColor
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
1
by: | last post by:
Would be nice to specify an incremenet or decremenet as an attribute in an enum so I can for example have the following... enum SomeEnum { A = 0, B, // 2 C, // 4 D /6
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: praveenkojha | last post by:
Hi, I am novice in C++ and am more of a C# guy. I have a third party C++ code which I want to create and use as a managed assembly. I have created a .NET win32 application and have copied this...
2
by: Stan | last post by:
Suppose I have this enum public class Flag { Hazardous = 1, Protected = 2 } and I have this webmethod:
3
by: Bob Harrison | last post by:
I am monitoring some electronic equipment. Every few seconds i poll for equipment status. The results are returned in 6 groups - like 0A 40 08 00 00 00. Currently only the first 3 bytes contain...
1
by: hello2008 | last post by:
Hi, I have just started coding in PHP. I have coded a web page using HTML, JS, and PHP. An HTML table has to be populated dynamically using the data from the backend. Presently I have 5...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.