Reputation: 71
Friends
How do I implement following complex logic?
flag1 can be "N" or "A" or "I"
flag2 can be "N" or "A" or "I"
flag3 can be "N" or "A" or "I"
function (string flag1, string flag2, string flag3) begin
The function needs to return:
return "None" if flag1, flag2 and flag3 are "N"
else return "Active" if flag1, flag2 and flag3 are "A"
else return "Inactive" if flag1, flag2 and flag3 are "I"
else return "both" if flag1, flag2 and flag3 are either "A" AND "I" (OR "N")
e.g. 1) flag1 is "A" and flag2 is "I" and flag3 is "I"
e.g. 2) flag1 is "I" and flag2 is "A" and flag3 is "I"
e.g. 2) flag1 is "A" and flag2 is "N" and flag3 is "I"
retrun result
end
Thanks for reply but none of post gives answer. I know if else constrauct and looking for logic to implement above psedocode. All four are possibel conditions specially #4 is complex and need to know how to implement that.
Upvotes: 6
Views: 1323
Reputation: 5967
[Flags]
enum SomeWierdReturn
{ Both = 0, None = 1, Active = 2, Inactive = 4 }
public SomeWierdReturn DoSomething(SomeWierdReturn flag1, SomeWierdReturn flag2, SomeWierdReturn flag3)
{
return (SomeWierdReturn)(flag1 & flag2 & flag3);
}
Upvotes: 2
Reputation: 9093
Robaticus gave you the right answer but in a comment rather than a post so I will expand upon it.
We have three flags that can take each of three states. There are thus 3 * 3 * 3 = 27 possible options.
When facing complex if..then logic and such a small address space it makes no sense at all to try to code all the if..then's. Instead, one three-dimensional array holding a total of 27 elements.
const int None = 0; const int Inactive = 1; const int Active = 2;
private int ParseFlag(string Flag) { switch (Flag) { case "N": return None; case "I": return Inactive; case "A": return Active; default throw new Exception(string.Format("Got a flag value of {0} but expected N, I or A", Flag)); } }
public FlagResult Lookup(string Flag1, string Flag2, string Flag3) { return FlagData[ParseFlag(Flag1), ParseFlag(Flag2), ParseFlag(Flag3)]; }
I'll let you build the array.
Upvotes: 0
Reputation: 58522
This would* work. Can't say I like it.
string key = flag1 + flag2 + flag3;
switch(key){
case "NNN":
return "None";
case "AAA":
return "Active";
case "III":
return "Inactive";
default:
break;
}
// ;)
//Trying to make it as confusing as your requirement #4
var four = (
from s in key
select s
).Distinct();
if(four.Count() > 1){
return "Both";
}
}
Upvotes: 0
Reputation: 46098
Your logic for point 4 is confusing...
I would use an enum value for this, rather than strings - it is much more type-safe (eg what if someone passed "WIBBLEWOBBLE"
to your method? What should it return?)
enum Value { None, Active, Inactive, Both }
private Value GetValue(Value flag1, Value flag2, Value flag3) {
if (flag1 == flag2 && flag2 == flag3) // they are all the same
return flag1;
else return Value.Both; // there is a difference
}
Upvotes: 20
Reputation: 273244
I think it serves both readability and speed if you first check if all 3 values are equal.
if ((flag1 == flag2) and (flag1 == flag3))
// use a switch or table to go 'I' -> Inactive etc
else
return "Both"; // as far as i understood 4)
Upvotes: 1