Doesn't go in all iff statements.

Discussion in 'ASP .Net' started by bbawa1@yahoo.com, Jun 21, 2007.

  1. Guest

    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";

    }
     
    , Jun 21, 2007
    #1
    1. Advertising

  2. 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 ;-)
    --
    Milosz


    "" wrote:

    > 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";
    >
    > }
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 22, 2007
    #2
    1. Advertising

  3. Hi,

    Milosz Skalecki [MCAD] wrote:
    > 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
     
    Laurent Bugnion, MVP, Jun 22, 2007
    #3
  4. 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, MVP" wrote:

    > Hi,
    >
    > Milosz Skalecki [MCAD] wrote:
    > > 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
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 22, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,232
    mfmehdi
    Oct 20, 2006
  2. cr88192
    Replies:
    3
    Views:
    566
    cr88192
    Nov 3, 2004
  3. Harry George
    Replies:
    6
    Views:
    444
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    775
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    289
    John Crichton
    Jul 12, 2010
Loading...

Share This Page