Go Pro!

Handling Plurals and Singulars

Reference > Science > Technology > Beginner Programming Tips

Okay, here's what I want to know: have you ever done a piece of code like the following?

Dim MyString As String
Dim Seconds As Integer
Dim Minutes As Integer

If Minutes = 1 Then
   MyString = MyString & Minutes & " minute"
   MyString = MyString & Minutes & " minutes"
End If

If Seconds = 1 Then
   MyString = MyString  & " and " & Seconds & "
   MyString = MyString & " and " & Seconds & "
End If

What does this piece of code do? It looks at the variable Seconds, and determines whether to use the singular form or the plural form of the word "second." Then it does the same with for the word "minutes."

So if Minutes = 1 and Seconds = 5, this code would produce the string: 1 minute and 5 seconds. If Minutes = 5 and Seconds = 1, it would produce the string: 5 minutes and 1 second.

The code works, but it's not very practical if you are going to be doing this kind of thing a lot!

Take a look at the following function.

Function Pluralize(SingularForm As String, _
   PluralForm As String, Count As Integer) As

   If Count = 1 Then
      Pluralize = SingularForm
      Pluralize = PluralForm
   End If
End Function

Here's how this works. You pass to the function both the singular and plural forms of the word you are dealing with, and an integer value indicating the count of objects.

Now let's take a look at how this works.

Dim MyString As String
Dim Seconds As Integer
Dim Minutes As Integer

MyString = Minutes & " " & _
   Pluralize("minute", "minutes", Minutes)

MyString = MyString & Seconds & " " & _
   Minutes & Pluralize("second", "seconds",

Isn't that going to make life a whole lot easier for you if you have to evaluate a lot of singular/plural expressions?

Another Version

Another version of this function could be created which only required you to pass the Count variable, and then the function simply returns an 's' if the word is supposed to be plural, or nothing if the word is supposed to be singular. Here's how the function would look:

Function Pluralize(Count As Integer) As String
   If Count = 1 Then
      Pluralize = ""
      Pluralize = "s"
   End If
End Function

Such a function would be used like this:

MyString = Seconds & " second" & Pluralize(Seconds)

This second version of the function seems like it might be better, since it saves having to pass two extra variables. However, what seems to be true is not always so. Why is this second version of the function not as good as the first one? 


Why is the second version of the function not as good?
Could you use this function to turn an adjective into an adverb (adding "ly"?)
Can you write a function that adds "st", "nd", "rd" or "th" based on a number (ie, 1st, 2nd, etc)?
Assign this reference page
Click here to assign this reference page to your students.
Seconds, Minutes, and HoursSeconds, Minutes, and Hours
Copy and Paste Rule of ThumbCopy and Paste Rule of Thumb

Understanding Coronavirus Spread

A Question and Answer session with Professor Puzzler about the math behind infection spread.

Blogs on This Site

Reviews and book lists - books we love!
The site administrator fields questions from visitors.
Like us on Facebook to get updates about new resources
Pro Membership