3
\$\begingroup\$
if (progressBar1.Value == 1) { progressLabel.Text = "1%"; }
if (progressBar1.Value == 10) { progressLabel.Text = "10%"; }
if (progressBar1.Value == 20) { progressLabel.Text = "20%"; }
if (progressBar1.Value == 30) { progressLabel.Text = "30%"; }
if (progressBar1.Value == 40) { progressLabel.Text = "40%"; }
if (progressBar1.Value == 50) { progressLabel.Text = "50%"; }
if (progressBar1.Value == 60) { progressLabel.Text = "60%"; }
if (progressBar1.Value == 70) { progressLabel.Text = "70%"; }
if (progressBar1.Value == 80) { progressLabel.Text = "80%"; }
if (progressBar1.Value == 90) { progressLabel.Text = "90%"; }
if (progressBar1.Value == 100) { progressLabel.Text = "100%"; }
\$\endgroup\$

closed as unclear what you're asking by Jamal Jul 8 '15 at 21:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I like dreza's answer, if you wanted to make this code more efficient you could use if..else if rather than several if statements. it would hit one, show it and exit the code until the next iteration of the code \$\endgroup\$ – Malachi Sep 13 '13 at 14:43
16
\$\begingroup\$

Why can't you do:

progressLabel.Text = string.Format("{0}%", progressBar1.Value)

As @MNZ pointed out I potentially didn't answer the question exactly how the OP asked. So here's a slightly modified alternative option:

progressLabel.Text = FormatProgress(progressBar1.Value, progressLabel.Text)

private string FormatProgress(int currentProgress, string progressDisplayed)
{
    if (currentProgress < 0 || currentProgress > 100)
        throw new ArgumentOutOfRangeException("Current progress is not in a valid percentage range of 0 to 100");

    if (currentProgress == 1) 
        return FormatProgress(currentProgress);

    return currentProgress % 10 == 0 ? 
              FormatProgress(currentProgress) : 
              progressDisplayed;
}

private string FormatProgress(int progress)
{
    return string.Format("{0}%", progress);
}

And a unit test to confirm:

[TestMethod]
public void Test()
{
    Assert.AreEqual("1%", FormatProgress(1, "0%"));
    Assert.AreEqual("0%", FormatProgress(0, "5%"));
    Assert.AreEqual("10%", FormatProgress(10, "6%"));
    Assert.AreEqual("80%", FormatProgress(85, "80%"));
    Assert.AreEqual("100%", FormatProgress(100, "90%"));
    Assert.AreNotEqual("75%", FormatProgress(75, "70%"));            
}
\$\endgroup\$
  • \$\begingroup\$ Although this code is not exactly same as the code in question. It must have an if satement to check if progressLabel.Value is one of 1,10,20,30,40,50,60,70,80,90 or 100. \$\endgroup\$ – MNZ Sep 14 '13 at 0:16
1
\$\begingroup\$

Use a switch statement...

switch(progressBar.Value)
{
case 1:
progressLabel.Text = "1%"; 
break;

case 10:
progressLabel.Text = "10%"; 
break;

case 20:
progressLabel.Text = "20%"; 
break;

case 30:
progressLabel.Text = "30%"; 
break;
}

etc...

\$\endgroup\$
  • \$\begingroup\$ this would be more code than the if statements. it wouldn't be shorter. \$\endgroup\$ – Malachi Sep 13 '13 at 14:41
  • \$\begingroup\$ @Malachi indeed, but it does address the multiple condition evaluation that you pointed out in your comment to the question, and is more elegant than a bunch of else blocks. \$\endgroup\$ – Mathieu Guindon Sep 13 '13 at 15:00
  • \$\begingroup\$ that is almost a toss up, but I think I am going to have to agree with you on that @retailcoder. \$\endgroup\$ – Malachi Sep 13 '13 at 15:06
  • 3
    \$\begingroup\$ Switch Statements are much more efficient than an else if. Else if statements force each if statement to be tested before finding the correct one. Case statements only test once. Plus it gives you more flexibility than drezas answer, although drezas IS much shorter. \$\endgroup\$ – Nate S. Sep 13 '13 at 15:28
  • \$\begingroup\$ @CodeSlinger and that's what got you my upvote ;) ...although it is overkill in this specific case. \$\endgroup\$ – Mathieu Guindon Sep 13 '13 at 16:17
1
\$\begingroup\$
if (progressBar1.Value == 1 || progressBar.Value % 10 == 0)
    progressLabel.Text = progressBar1.Value.ToString() + "%";

Assuming that you are using a standard WinForms progress bar, you shouldn't have to worry about the progressBar.Value being <0 or >100.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.