473,378 Members | 1,555 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,378 software developers and data experts.

How to retrieve multiple enum values?

15
Let's say I have an enumeration like so:

[Flags]
public enum Letters
{
A = 0,
B = 1,
C = 2,
D = 4,
E = 8
}

And let's say I have a variable like so:

Letters phrase = Letters.A | Letters.B | Letters.C;

How would I check to see if "phrase" contains Letter.A, Letter.B, and Letter.C individually?

e.g. The following code does not work:

if (phrase == Letters.A)
{
// some stuff...
}
if (phrase == Letters.B)
{
}
SOLVED

Solution

if ((phrase & Letters.A) == Letters.A)
{
// true
}
else
{
// false
}
Mar 15 '10 #1
2 3381
tlhintoq
3,525 Expert 2GB
You have to do a additive check for the bit

Expand|Select|Wrap|Line Numbers
  1. if ((phrase & Letters.A) == Letters.A)
  2. {
  3.    // You have a match
  4. }
To make life easy and reduce typing you might want to make a method for checking

Expand|Select|Wrap|Line Numbers
  1. private bool IsLettersA(string CheckPhrase)
  2. {
  3.     if ((CheckPhrase & Leters.A) == Letters.A) return true;
  4.     else return false
  5. }
Now you can check like this

Expand|Select|Wrap|Line Numbers
  1. if (IsLettersA(Phrase))
  2. {
  3.    // So something if it is a match
  4. }
Mar 15 '10 #2
PRR
750 Expert 512MB
You can do bitwise OR or AND
Enumeration Types
Mar 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
4
by: marco.nl | last post by:
it fails to retrieve newly intruduced data by the user. i tried this ... AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); UpdateData(FALSE); and this.....
5
by: Tim Gallivan | last post by:
I have 2 functions, CheckThis and CheckThat. Each function can return some of the error codes in the Enum below (CheckThis can return 0, 1, 2 or 3 and CheckThat can return 0, 4, 5 or 6). I'm trying...
1
by: ykong1214 | last post by:
In my project, there is a single select tag, <html:select property="userName" size="7"> <html:options collection="UserList" property="value" labelProperty="label" /> </html:select> ...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
3
by: Jens Müller | last post by:
I have a file here with several enums: #ifndef PLANARSEP_OPTIMIZE_H #define PLANARSEP_OPTIMIZE_H enum fund_cycle_behavior_t {PASS_MODE_FIRST, PASS_MODE_BEST, PASS_MODE_ALL};
2
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma...
1
by: Murdz | last post by:
Hi all, A constructor for System.Text.RegularExpressions.Regex includes a 2nd parameter for the RegexOptions enum. With this you can pass multiple RegexOptions as below: ...
1
by: benmanns | last post by:
I am writing a web application with a MySQL backend that will have multiple users taking various surveys. Each survey has a different number of questions; each question has two types of answers:...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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.