Doesn't go in all iff statements.

B

bbawa1

Hi,

I have the following code. but if just goes to first if Statement
where I make comparison. but it doesn't go to second if statement.
Colul u please help me with that.


protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)

//DateTime enddate = DateTime.Now;
//TimeSpan duration = enddate -
Convert.ToDateTime(e.Row.Cells[1]);

if ((Convert.ToInt32(e.Row.Cells[2].Text) >= 17) &&
(Convert.ToInt32(e.Row.Cells[2].Text) <= 23))
{

e.Row.Cells[0].CssClass = "sdgStatusOrange";

}
if ((Convert.ToInt32(e.Row.Cells[2].Text) >= 27) &&
(Convert.ToInt32(e.Row.Cells[2].Text) <=29))

{

e.Row.Cells[0].CssClass = "sdgStatusYellow";

}
if ((Convert.ToInt32(e.Row.Cells[2].Text) >= 30) &&
(Convert.ToInt32(e.Row.Cells[2].Text) <=32))

{

e.Row.Cells[0].CssClass = "sdgStatusBlue";

}
 
G

Guest

Hi there,

C# supports short circuit evaluation, meaning it evaluates its second
operand if necessary, i.e. for && operand only if the first operand is true.

a && b == true only if a and b are true (therefore, if a is false there is
no point to check the second operand)

a || b == true for all cases except a and b are false. Therefore b is tested
only if a equals false

Should be clear now ;-)
 
L

Laurent Bugnion, MVP

Hi,
Hi there,

C# supports short circuit evaluation, meaning it evaluates its second
operand if necessary, i.e. for && operand only if the first operand is true.

a && b == true only if a and b are true (therefore, if a is false there is
no point to check the second operand)

a || b == true for all cases except a and b are false. Therefore b is tested
only if a equals false

Should be clear now ;-)

Which can, BTW, be very dangerous if the developer is not aware of that.

For example:

private bool doSomething()
{
// ... Do something
}

if ( myBoolean || doSomething() )
{
// Bla bla
}

In the case above, if myBoolean is true, then the method doSomething is
never called, which can cause unexpected behaviour.

Greetings,
Laurent
 
G

Guest

Hi Laurent,

Yes, I definitely agree. But these are basics, and every C# developer should
be (in theory ;-) aware about short-circuit evaluation.

Best regards

--
Milosz


Laurent Bugnion said:
Hi,
Hi there,

C# supports short circuit evaluation, meaning it evaluates its second
operand if necessary, i.e. for && operand only if the first operand is true.

a && b == true only if a and b are true (therefore, if a is false there is
no point to check the second operand)

a || b == true for all cases except a and b are false. Therefore b is tested
only if a equals false

Should be clear now ;-)

Which can, BTW, be very dangerous if the developer is not aware of that.

For example:

private bool doSomething()
{
// ... Do something
}

if ( myBoolean || doSomething() )
{
// Bla bla
}

In the case above, if myBoolean is true, then the method doSomething is
never called, which can cause unexpected behaviour.

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top