enums
-
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
//execute this block
}becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
//execute this block
}
// the goal of this if pp has a value of prescription or both it will execute.thank you so much for the some one who shed me some light
-
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
//execute this block
}becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
//execute this block
}
// the goal of this if pp has a value of prescription or both it will execute.thank you so much for the some one who shed me some light
I would do this:
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
//Both = PrescriptionOnly | InstructionOnly
}and then:
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.InstructionOnly))
{
//execute this block
}I think that should work. In any case using "both" as naming convention is a bit confusing. I'm also not sure about the behaviour of the both definition in the enum definition.
V.
(MQOTD rules and previous solutions) -
I would do this:
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
//Both = PrescriptionOnly | InstructionOnly
}and then:
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.InstructionOnly))
{
//execute this block
}I think that should work. In any case using "both" as naming convention is a bit confusing. I'm also not sure about the behaviour of the both definition in the enum definition.
V.
(MQOTD rules and previous solutions)i dont know if using the name "both" is the right name to use, but it will act as it has the value of "PrescriptionOnly" and "InstructionONly"
-
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
//execute this block
}becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
//execute this block
}
// the goal of this if pp has a value of prescription or both it will execute.thank you so much for the some one who shed me some light
If you think of it in terms of binary:
PrescriptionOnly = 01
InstructionOnly = 10
Both = 11And you want either PrescriptionOnly or Both:
01 or 11
So, of the four possible states the variable can be in, you want two:
None 00 X
PrescriptionOnly 01 Y
InstructionOnly 10 X
Both 11 YSo all you have to do is:
if ((pp & PrescriptionTypePrint.PrescriptionOnly) != 0)
{
...Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
//execute this block
}becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
//execute this block
}
// the goal of this if pp has a value of prescription or both it will execute.thank you so much for the some one who shed me some light
Use another binary operator
if( (pp & PrescriptionTypePrint.Both) != 0)
{
// execute this block
}This will binary-AND two integer operands. The result will equal zero if the oparands don't share a common bit set to '1'. Therefore 'pp' will not result in the expression to equal zero whenever any one of the bits of 'Both' are set in 'pp'.
Ciao, luker
-
Use another binary operator
if( (pp & PrescriptionTypePrint.Both) != 0)
{
// execute this block
}This will binary-AND two integer operands. The result will equal zero if the oparands don't share a common bit set to '1'. Therefore 'pp' will not result in the expression to equal zero whenever any one of the bits of 'Both' are set in 'pp'.
Ciao, luker
Um...that'll also execute it for the InstructionOnly case, won't it?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Um...that'll also execute it for the InstructionOnly case, won't it?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Yep, it will. Your questioning seems like that's not what you wanted. Looking again at what you wrote, I think that's what you need:
if( ( pp & PrescriptionOnly ) != 0 )
{
// execute in case of PrescriptionOnly or Both
// Since Both sets, among others, the PrescriptionOnly-bit.
}Ciao, luker
-
Yep, it will. Your questioning seems like that's not what you wanted. Looking again at what you wrote, I think that's what you need:
if( ( pp & PrescriptionOnly ) != 0 )
{
// execute in case of PrescriptionOnly or Both
// Since Both sets, among others, the PrescriptionOnly-bit.
}Ciao, luker
Um...you mean what the OP wanted? That is what I had already suggested... :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Um...you mean what the OP wanted? That is what I had already suggested... :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Sorry, Griff. Yes, I meant the OP. And Wow, your answer is exhaustive. I'll give it an up.
Ciao, luker
opsss, I miss the conversation, sorry i was out for a few days. By the way THANK YOU SO MUCH for the suggestions, surely I will try it.
-
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
//execute this block
}becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
//execute this block
}
// the goal of this if pp has a value of prescription or both it will execute.thank you so much for the some one who shed me some light
A slightly different approach using Exclusive-Or:
[Flags]
enum testEnum
{
p1 = 1,
p2 = 2,
p3 = 3,
p4 = 4,
p5 = 5
}private void TestExOr()
{
testEnum t1 = testEnum.p3;testEnum t2 = testEnum.p5; // true bool test1 = t1 == (testEnum.p1 ^ testEnum.p2); bool test2 = t2 == (testEnum.p1 ^ testEnum.p4); bool test3 = t2 == (testEnum.p0 ^ testEnum.p5); // false bool test4 = t1 == (testEnum.p2 ^ testEnum.p3); bool test5 = t2 == (testEnum.p2 ^ testEnum.p3); bool test6 = t2 == (testEnum.p5 ^ testEnum.p4); bool test7 = t2 == (testEnum.p5 ^ testEnum.p1); // is the false result here a deal-breaker ? bool test8 = t2 == (testEnum.p5 ^ testEnum.p5);}
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
-
A slightly different approach using Exclusive-Or:
[Flags]
enum testEnum
{
p1 = 1,
p2 = 2,
p3 = 3,
p4 = 4,
p5 = 5
}private void TestExOr()
{
testEnum t1 = testEnum.p3;testEnum t2 = testEnum.p5; // true bool test1 = t1 == (testEnum.p1 ^ testEnum.p2); bool test2 = t2 == (testEnum.p1 ^ testEnum.p4); bool test3 = t2 == (testEnum.p0 ^ testEnum.p5); // false bool test4 = t1 == (testEnum.p2 ^ testEnum.p3); bool test5 = t2 == (testEnum.p2 ^ testEnum.p3); bool test6 = t2 == (testEnum.p5 ^ testEnum.p4); bool test7 = t2 == (testEnum.p5 ^ testEnum.p1); // is the false result here a deal-breaker ? bool test8 = t2 == (testEnum.p5 ^ testEnum.p5);}
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
Thank you so much I will try it.