I should know how to do this but I unfortunately don't, so I'd just like to clarify...
I've got a Flags enum: - [Flags]
-
public enum SkillSet
-
{
-
Skill1 = 1,
-
Skill2 = 2,
-
Skill3 = 4,
-
Skill4 = 8,
-
Skill5 = 16,
-
Skill6 = 32,
-
Skill7 = 64,
-
Skill8 = 128
-
}
-
I then have for example: -
SkillSet skillsAvaliable = Skill1 && Skill2 && Skill7 && Skill6; // believe this is correct?
-
SkillSet skillsRequired = Skill1 && Skill7;
I now want to see if all the skillsRequired are contained within skillsAvaliable. Is it possible to do this in one call? Or do I need to loop through each value of skillsRequired compare to the skillsAvaliable and return the resultant bool?
7 1523
EDIT:
I *think* you can do bit logic, something like this: -
//given:
-
SkillSet skillsAvaliable1 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill7 | SkillSet.Skill6;
-
SkillSet skillsAvaliable2 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill6;
-
SkillSet skillsRequired = SkillSet.Skill1 | SkillSet.Skill7;
-
-
//then do this
-
bool hasCorrectSkills1 = ((skillsAvaliable1 & skillsRequired) == skillsRequired);
-
bool hasCorrectSkills2 = ((skillsAvaliable2 & skillsRequired) == skillsRequired);
-
MessageBox.Show(hasCorrectSkills1.ToString() + " - " + hasCorrectSkills2.ToString());
You need to be using the single or '|'
- [Flags]
-
public enum SkillSet
-
{
-
Skill1 = 1,
-
Skill2 = 2,
-
Skill3 = 4,
-
Skill4 = 8,
-
Skill5 = 16,
-
Skill6 = 32,
-
Skill7 = 64,
-
Skill8 = 128
-
}
-
It makes perfect sense if you take a look at it from the view of bytes.
Skill1 = 0000 0001
Skill2 = 0000 0010
Skill3 = 0000 0100
Skill4 = 0000 1000
Skill5 = 0001 0000
Skill6 = 0010 0000
Skill7 = 0100 0000
Skill8 = 1000 0000
Now if skills available has 1, 2, 7, and 6 then you would have:
Skill1 | Skill2 | Skill7 | Skill 6
0000 0001
0000 0010
0010 0000
0100 0000
_________
0110 0011
SkillsAvailable = 0110 0011
Skills Required would then need Skills 1 and 7 so:
Skill1 | Skill7
0100 0000
0000 0001
_________
0100 0001
SkillsRequired = 0100 0001
Then to see if SkillsAvailable has SkillsRequired you would AND them together and then check if it matched SkillsRequired.
SkillsAvailable & SkillsRequired
0100 0001
0110 0011
__________
0100 0001
SkillsMix = 0100 0001
SkillsMix == SkillsRequired
Hope that explains it
TRScheel thanks for that, it makes a lot of sense if you look at it from that perspective and actually makes it seem incredibly easy!
Thanks for you response Plater, I think however I'm going to follow TRScheel's suggestion for simplicity and readability :D
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
Mine has bit math, hence its truthiness factor is way higher
=D
Mine has bit math, hence its truthiness factor is way higher
=D
I gave a man a fish. You taught him to fish.
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
Actually Plater, having looked back I believe your post is equally useful as I skimmed over it too quickly. You're explaining that I should be using the | OR rather than & AND which is vital really!
I take it back, both are good in their own way :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Carl |
last post by:
Hi,
I have found a way to map attributes (columns) to column headings. But
this runs really slow. Is there a way to improve it?
Thanks,
Carl
<?xml version="1.0" encoding="utf-8" ?>...
|
by: Keith Hill |
last post by:
Is there a way to represent a style enum in XML Schema? Say I have
a flags enum like so:
public enum dayOfWeek {
Mon,
Tue,
Wed,
All = Mon | Tue | Wed;
}
|
by: Patrick Blackman |
last post by:
Need example of how to use the flags attribute and to determine which flags
were set.
|
by: Paul E Collins |
last post by:
The help file says that "bit fields can be combined using a bitwise OR operation, whereas
enumerated constants cannot", and yet this code works:
enum Blah { a, b, c }; // without
....
Blah x...
|
by: Joel Moore |
last post by:
Say I have an enum similar to this:
<Flags()> Enum TestFlags As Short
One = 0
Two = 1
Three = 2
Four = 4
End Enum
I figured I could declare variable of type TestFlags to be used in the
|
by: Chad |
last post by:
I've used bitwise enums before (powers of 2) and have used AND and OR operator to determine which individual flags have been set, but I have never used the <Flags()> _ attribute
What advantage...
|
by: martin |
last post by:
Hi,
I have used bitfields in C before and I understand all the AND/OR operations
etc. While learning C# I was told to mark all by "bitfield enums" with the
attribute. Do why? Exactly what does...
|
by: Jigar.Patel |
last post by:
Hi,
I have following enum definition.
public enum OperationTypes
{
All = 1,
New = 2,
View = 4,
|
by: Plissken.s |
last post by:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool...
|
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
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |