Community Code Offering: Singular/Plural Phrase Constructor


G

Guest

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);
}
 
Ad

Advertisements

C

Cool Guy

Robert W. said:
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:
[snip]

One problem with your code is that it fails when the noun plural form isn't
made by simply adding an 's' to the singular form (for example: bacterium
--> bacteria).

I wrote a somewhat similar library routine. It goes something like this
(note that the parameters are assumed to be valid here):

public static string GetQuantityPhrase(int num, string singular,
string plural) {
string noun = num == 1 ? singular : plural;
return String.Format("{0} {1}", num.ToString(), noun);
}
 
Ad

Advertisements

G

Guest

True, and I'm sure there are countless other English examples where it would
fail. But it handles 99% of the times I need it.

--
Robert W.
Vancouver, BC
www.mwtech.com



Cool Guy said:
Robert W. said:
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:
[snip]

One problem with your code is that it fails when the noun plural form isn't
made by simply adding an 's' to the singular form (for example: bacterium
--> bacteria).

I wrote a somewhat similar library routine. It goes something like this
(note that the parameters are assumed to be valid here):

public static string GetQuantityPhrase(int num, string singular,
string plural) {
string noun = num == 1 ? singular : plural;
return String.Format("{0} {1}", num.ToString(), noun);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top