c# - Code is unreachable -
relatively new programming can't work out why if statement unreachable. best way return calculation can send back?
while (reader.read()) { if (reader.nodetype == xmlnodetype.element) { if (reader.name == "amount") { if ("amount" == "gold" || value > 4999) { credit = 300; discountpercent = .20f; } else if ("amount" == "silver" || value > 4999) { discountpercent = .15f; } else if ("amount" == "regular") { credit = 200; } } else if (reader.name == "member") { if ("member" == "gold" || value > 4999) { credit = 300; discountpercent = .20f; } else if ("member" == "silver" || value > 4999) { discountpercent = .15f; } else if ("member" == "regular") { credit = 200; } } } } }
could because i'm not including break statement?
"amount" == "gold"
never evaluate true
, because strings different. same goes other string comparisons. compiler reduces false
out of ||
, "sees" following:
if (value > 4999) { credit = 300; discountpercent = .20f; } else if (value > 4999) { discountpercent = .15f; } else if (false) { credit = 200; }
the compiler reasonably concludes middle , bottom if
s unreachable.
to fix code, read value, , use in comparisons:
if (reader.name == "amount") { var amount = reader.value; if (amount == "gold" || value > 4999) { credit = 300; discountpercent = .20f; } else if (amount == "silver" || value > 4999) { discountpercent = .15f; } else if (amount == "regular") { credit = 200; } }
Comments
Post a Comment