what is a more simple way to do this if statement

Answered what is a more simple way to do this if statement

  • Friday, February 24, 2012 12:10 AM
     
     
    if (newgame == false && button1.Text !=""&& button2.Text != ""&&button3.Text !=""&&button4.Text !=""&&button5.Text != ""&&button6.Text !=""&&button7.Text !=""&&button8.Text!=""&&button9.Text !="")
                {
                   
                }

All Replies

  • Friday, February 24, 2012 12:18 AM
    Moderator
     
     Answered Has Code

    The code you have is fairly straightforward.

    If you stored your button references in a list, you could simplify it:

    // Define this somewhere - it could be once in your constructor
    List<Button> buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7, button8, button9 };
    
    // Given the above, your if becomes:
    
    if (newgame == false && buttons.All(b => b.Text != ""))
    {
    }

    This is really only useful if you're doing multiple checks like this, though...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by kosswhite Friday, February 24, 2012 12:23 AM
    •