Search:
Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

Those Pesky Plurals

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"
Else
   MyString = MyString & Minutes & " minutes"
End If

If Seconds = 1 Then
   MyString = MyString  & " and " & Seconds & " second"
Else
   MyString = MyString & " and " & Seconds & " 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 String
   
   If Count = 1 Then
      Pluralize = SingularForm
   Else
      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", 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?

Other Scenarios

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 = ""
   Else
      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?

Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page