using flag attribute on an enum

J

John Salerno

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

stigsen

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

Daniel O'Connell [C# MVP]

John Salerno said:
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
 
J

John Salerno

Daniel said:
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?
 
D

Daniel O'Connell [C# MVP]

John Salerno said:
Daniel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top