473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Community Code Offering: Singular/Plural Phrase Constructor

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

Nov 17 '05 #1
2 2286
"Robert W." <Ro*****@discussions.microsoft.com> wrote:
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);
}
Nov 17 '05 #2
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" wrote:
"Robert W." <Ro*****@discussions.microsoft.com> wrote:
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);
}

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
3
by: Jack | last post by:
Hi, I have the following code which addes a publisher to the publishers table in the pubs database. However, for some reason, I am getting the following error: Error Type: Microsoft VBScript...
7
by: Bosconian | last post by:
This might seem like a trivial thing, but has anyone has come up with a better way of outputting a singular vs. plural string? For example: // default plural label $string = "appointments";...
2
by: davjoh123 | last post by:
if ($result) { $rs->datums = mysql_fetch_array ($result, MYSQL_ASSOC); What does -do after the recordset variable $rs? Is datums a field name or something else? I have a text by Larry...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.