Better way of coding this if statment?

Posted by HadlowJ on Stack Overflow See other posts from Stack Overflow or by HadlowJ
Published on 2011-01-03T20:48:04Z Indexed on 2011/01/03 20:53 UTC
Read the original article Hit count: 207

Filed under:
|
|
|

Hi. I have another question for you very helpful people. I use a lot of if statements many of which are just repeated and im sure could be shortened. This is my current bit of code

        if (Globals.TotalStands <= 1)
        {
            ScoreUpdate.StandNo2.Visible = false;
            ScoreUpdate.ScoreStand2.Visible = false;
            ScoreUpdate.ScoreOutOf2.Visible = false;
        }

        if (Globals.TotalStands <= 2)
        {
            ScoreUpdate.StandNo3.Visible = false;
            ScoreUpdate.ScoreStand3.Visible = false;
            ScoreUpdate.ScoreOutOf3.Visible = false;
        }

        if (Globals.TotalStands <= 3)
        {
            ScoreUpdate.StandNo4.Visible = false;
            ScoreUpdate.ScoreStand4.Visible = false;
            ScoreUpdate.ScoreOutOf4.Visible = false;
        }

        if (Globals.TotalStands <= 4)
        {
            ScoreUpdate.StandNo5.Visible = false;
            ScoreUpdate.ScoreStand5.Visible = false;
            ScoreUpdate.ScoreOutOf5.Visible = false;
        }

        if (Globals.TotalStands <= 5)
        {
            ScoreUpdate.StandNo6.Visible = false;
            ScoreUpdate.ScoreStand6.Visible = false;
            ScoreUpdate.ScoreOutOf6.Visible = false;
        }

        if (Globals.TotalStands <= 6)
        {
            ScoreUpdate.StandNo7.Visible = false;
            ScoreUpdate.ScoreStand7.Visible = false;
            ScoreUpdate.ScoreOutOf7.Visible = false;
        }

        if (Globals.TotalStands <= 7)
        {
            ScoreUpdate.StandNo8.Visible = false;
            ScoreUpdate.ScoreStand8.Visible = false;
            ScoreUpdate.ScoreOutOf8.Visible = false;
        }

as you can see there is a huge amount of code to do something simple (which i do on a few other forms as well and im sure there must be a better way of coding this that gets the same result? Im a code noob so please be gentle, code is c# and software is visual studio 2008 pro. Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about if-statement