2

I have another question for you very helpful people. I use a lot of if statements many of which are just repeated and I'm 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 I'm sure there must be a better way of coding this that gets the same result? I'm a code noob so please be gentle, code is C# and software is Visual studio 2008 pro.

1
  • Sorry but which language are you using? This could be one of several. Commented Jan 3, 2011 at 20:50

2 Answers 2

8

Most of your properties should be arrays (or some other collection). For example:

ScoreUpdate.StandNo6

could be this instead:

ScoreUpdate.StandNos[5]

Then you can use a loop instead of all those if statements:

for (int i = 0; i < Globals.TotalStands; ++i)
{
    ScoreUpdate.StandNos[i].Visible = true;
    ScoreUpdate.ScoreStands[i].Visible = true;
    ScoreUpdate.ScoreOutOfs[i].Visible = true;
}

Or this slight variation might be better where there is an array of ScoreUpdates rather than three separate arrays:

for (int i = 0; i < Globals.TotalStands; ++i)
{
    var scoreUpdate = ScoreUpdates[i];
    scoreUpdate.StandNo.Visible = true;
    scoreUpdate.ScoreStand.Visible = true;
    scoreUpdate.ScoreOutOf.Visible = true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

how do i go about changing my properties to arrays. e.g. ScoreUpdate.StandNo6 is a label on another form? Thansk again
HadlowJ: To change them you'd have to add the controls to your form dynamically rather than doing it in the designer. But you don't actually need to change anything, you can also just add an array containing references to your existing controls. You can populate it in the constructor by doing a bunch of assignments.
1

You should make three arrays or lists of controls and use a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.