468,780 Members | 2,305 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

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 5880
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Woon Kiat | last post: by
2 posts views Thread by praveenkojha | last post: by
2 posts views Thread by Stan | last post: by
3 posts views Thread by Bob Harrison | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.