In my current work I noticed that I have several circumstances where I need
to create little if/else constructs to handle the phrasing of a message.
This typically involves a ternary situation like this example illustrates:
0 files were downloaded (or No files were downloaded)
1 file was downloaded
2 files were downloaded
They're all very similar but each is subtlely different. I soon realized
that I could write a few related methods to simplify this repetitive work.
And I'm presenting this code to the everyone here as a small token of my
gratitude for all the tremendous help I've received since I started
programming in C# in March. I think it's self explanatory but don't hesitate
to pose questions or suggestions:
public static string PrepareCorrectTense(int quantity, string noun,
string verbSingular, string verbPlural, bool noFlag)
{
string phrase;
verbSingular = (verbSingular == null) ? "" : verbSingular;
verbPlural = (verbPlural == null) ? "" : verbPlural;
if (quantity == 0)
phrase = ((noFlag == true) ? "No" : "0") + " " + noun + "s " +
verbPlural;
else if (quantity == 1)
phrase = "1 " + noun + " " + verbSingular;
else
phrase = quantity.ToString() + " " + noun + "s " + verbPlural;
return phrase.Trim();
}
public static string PrepareCorrectTense(int quantity, string noun,
string verbSingular, string verbPlural)
{
return PrepareCorrectTense(quantity, noun, verbSingular, verbPlural,
false);
}
public static string PrepareCorrectTense(int quantity, string noun)
{
return PrepareCorrectTense(quantity, noun, "", "", false);
}
--
Robert W.
Vancouver, BC
www.mwtech.com