473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6074
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.Get Values( typeof(Status) ) )
if( total >= (int)s )
result=s.ToStri ng();

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******@NOSPA Mgmail.com> wrote in message
news:3p******** ************@rc n.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******@NOSPA Mgmail.com> wrote in message
news:3p******** ************@rc n.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******@NOSPA Mgmail.com> wrote in message
news:Us******** ************@rc n.net...
Daniel O'Connell [C# MVP] wrote:
"John Salerno" <jo******@NOSPA Mgmail.com> wrote in message
news:3p******** ************@rc n.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.Bl ue));

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
5102
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
6596
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
8
3994
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( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
1
1609
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
3937
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
1513
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 C++ code here and made changes according to my needs. However I am always getting this linker error when compiling. I looked a lot n the web and followed all instructions so far of adding msvcrt.lib, glu32.lib, opengl32.lib to the project...
2
1488
by: Stan | last post by:
Suppose I have this enum public class Flag { Hazardous = 1, Protected = 2 } and I have this webmethod:
3
1902
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 meaningful info each bit reports either normal operation (a 0) or an error condition (a 1) for a component of the equipment. I set up <flag> Enum for each byte (just the first 3 bytes for now) Enum Byte1 None=0
1
5222
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 records in the backend table so I get 5 HTML-table rows. I have created a session object starting from the login page. The requirement is that as soon as I log in and my request gets forwarded to the foll PHP page I should be seeing the foll. dynamically...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.