Juan,
Come on, i can't see any logical exlanation in changing && operator to & for
this case
. First of all, logically, his "if" statement could be
simplified changed to:
if (Convert.ToInt32(e.Row.Cells[2].Text) >= 17)
{
e.Row.Cells[0].CssClass = "sdgStatusOrange";
}
simply because the only numbers that meet both criteria are >= 17. Second,
if the first operand (Convert.ToInt32(e.Row.Cells[2].Text) >= 15) would
return false, second could not be true (logically). Please also note he used
&& therefore there is not point to test the second operand as you're trying
to point out. Third, it was definitely compiler error (syntax error) which
has nothing to do with runtime evaluation.
Take it easy mate ;-)
Best regards
--
Milosz
:
re:
!> Did you mean if the text in cell equals "14"
Yes.
re:
!> then statement inside is not going to be reached, simply because the first
!> condition is false, and short-circuit evaluation for && operator skips the second operand.
That's exactly what I explained...and that's an undesirable "feature".
That's why he needs to change the operator.
re:
!> I understand your confusion
There's no confusion.
He should change both the parens *and* the operator,
the first as you suggest; the second as I suggest.
Doing that will cover all the bases for him.
Good morning Juan
Did you mean if the text in cell equals "14"
if (
(Convert.ToInt32(e.Row.Cells[2].Text) >= 15) &&
(Convert.ToInt32(e.Row.Cells[2].Text) >= 17))
{
e.Row.Cells[0].CssClass = "sdgStatusOrange";
}
then statement inside is not going to be reached, simply because the first
condition is false, and short-circuit evaluation for && operator skips the
second operand. I understand your confusion as I have seen this guy's
previous post, but in this case it was just misplaced parentheses ;-)
--
Milosz
:
Hi, Milosz,
In that case, what happens if Convert.ToInt32(e.Row.Cells[2].Text = 14) ?
Hi Juan,
In this case, "invalid expression term &&" is caused definitely by misplaced
parentheses, because it is a compiler message ;-) (evaluation would be done
at the runtime). Second, bitwise and & operator will give the same result
(apart from the fact short-circuit is not apllicable) because true & true =
true, false & true = false, false & false = false. Therefore, it should be
corrected to:
if (
(Convert.ToInt32(e.Row.Cells[2].Text) >= 15) &&
(Convert.ToInt32(e.Row.Cells[2].Text) >= 17))
{
e.Row.Cells[0].CssClass = "sdgStatusOrange";
}
Best regards
--
Milosz
:
re:
((Convert.ToInt32(e.Row.Cells[2].Text)>=15))&&(Convert.ToInt32(e.Row.Cells[2].Text)>=17);
You need to use the "regular" AND operator.
Try :
((Convert.ToInt32(e.Row.Cells[2].Text)>=15))&(Convert.ToInt32(e.Row.Cells[2].Text)>=17);
If you use && and e.Row.Cells[2].Text)>=15 is false, e.Row.Cells[2].Text)>=17
is not evaluated (because the result of the AND operation is false no matter what the
value of e.Row.Cells[2].Text)>=17 may be).
This is known as "short-circuit" evaluation...but it requires *both* operands to be
true.
In your code, that is not alsways the case.
It says invalid expression term &&
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if
((Convert.ToInt32(e.Row.Cells[2].Text)>=15))&&(Convert.ToInt32(e.Row.Cells[2].Text)>=17);
{
e.Row.Cells[0].CssClass = "sdgStatusOrange";
}
It says invalid expression term &&