dimanche 27 septembre 2020

How can I get rid of these else if statements and make the code look better?

I have this c# code

progressBar1.Increment(1);
if (progressBar1.Value > 5 && progressBar1.Value < 10)
{
  label1.Text = "Some text...";
  timer1.Stop();
  timer1.Start();
  timer1.Interval = 1000;
}
else if (progressBar1.Value > 10 && progressBar1.Value < 15)
{
  label1.Text = "Some text 2...";
  timer1.Interval = 250;
}
else if (progressBar1.Value > 15 && progressBar1.Value < 30)
{
  label1.Text = "Some text 3...";
}
else if (progressBar1.Value > 30 && progressBar1.Value < progressBar1.Maximum)
{
  label1.Text = "Some text 4...";
  timer1.Interval = 100;
}
else if (progressBar1.Value == progressBar1.Maximum)
{
  timer1.Stop();
  const string message = "Some cool popup message";
  const string caption = "Test";
  MessageBox.Show(message, caption, MessageBoxButtons.OK);
  Close();
}

I'm sure this code is bad, I'm not really a programmer and I just tried to create some fun application. How can I get rid of these long else if statements and replace it with something better? I've been looking for answers & tips but none of them helped me.

Aucun commentaire:

Enregistrer un commentaire