I have a tool with a configurable delay (Timespan), and I want to set the text of a label depending on the value. Here is my code as it stands:
StringBuilder time = new StringBuilder();
if (Settings.Default.WaitPeriod.Hours > 0)
{
time.AppendFormat("{0} hour(s)", Settings.Default.WaitPeriod.Hours);
}
if (Settings.Default.WaitPeriod.Minutes > 0)
{
time.AppendFormat("{0}{1} minute(s)", time.Length > 0 ? " and " : "", Settings.Default.WaitPeriod.Minutes);
}
if (Settings.Default.WaitPeriod.Seconds > 0)
{
time.AppendFormat("{0}{1} second(s)", time.Length > 0 ? " and " : "", Settings.Default.WaitPeriod.Seconds);
}
this.questionLabel.Text = String.Format("What have you been doing for the past {0}?", time);
I find the "(s)" particulary irritating - but don't want a million if/ternary if statements.
So, ignoring the non-localized strings, can you think of a nicer way to do this?