Reputation: 35
Here is the code what I have tried:
if(!string.IsNullOrEmpty(Taskinfo.DoRollOver) &&(Taskinfo.DoRollOver != "YES" || Taskinfo.DoRollOver != "NO"))
{
throw new DataException("DoRollOver is not valid");
}
But if I try to give the valid data such as YES
or NO
still it throws an exception.
Upvotes: 1
Views: 1795
Reputation: 861
I'd say that you inverted logic a little bit.
if((Taskinfo.DoRollOver == "YES")||(Taskinfo.DoRollOver == "NO"))
{
// do something
}
else
throw new DataException("DoRollOver is not valid");
Also if TaskInfo.DoRollOver is string I would use TaskInfo.DoRollOver.ToUpper() - just in case.
Upvotes: 1
Reputation: 679
Try to change conditions like this
if (string.IsNullOrEmpty(Taskinfo.DoRollOver) ||(Taskinfo.DoRollOver != "YES" && T0askinfo.DoRollOver != "NO"))
{
throw new DataException("DoRollOver is not valid");
}
Upvotes: 1
Reputation: 311163
This condition is always true. If Taskinfo.DoRollOver
is "YES" it is not equal to "NO", and vise-versa. You should use the &&
logical operator, not the ||
logical operator:
if (string.IsNullOrEmpty(Taskinfo.DoRollOver) ||
(Taskinfo.DoRollOver != "YES" && // Here!
T0askinfo.DoRollOver != "NO"))
{
throw new DataException("DoRollOver is not valid");
}
Upvotes: 2
Reputation: 21
maybe this can help you
if(!string.IsNullOrEmpty(Taskinfo.DoRollOver))
{
if(Taskinfo.DoRollOver != "YES" && Taskinfo.DoRollOver != "NO")
{
throw new DataException("DoRollOver is not valid");
}
}else
{
//Do Something
}
Upvotes: 1
Reputation: 37347
Aiming for readability and simplicity I would split conditions:
if(Taskinfo.DoRollOver == "YES")
{
// do smoething
}
else if(Taskinfo.DoRollOver == "NO")
{
// do smoething else
}
else
throw new DataException("DoRollOver is not valid");
Upvotes: 1