FIT 100

Lab Activity 10:

“What’s Your Sign?” Made Smarter

Winter  2002

PDF version

FIT100 home

 

Recommended Reading for Lab 10:

·         Chapter 13 from the FIT book

·         VB book: Ch.6

 

Introduction:

Today you will improve your SignFinder, so that is can report more precise errors to the user.  For example, it can indicate that June 31st is an incorrect date.

 

Objectives:

 

TO DO: 

  1. Today you will continue working with a copy of the code that you created previously.  Copy the Lab9 folder from your Dante account to the local machine.  Rename it into Lab10.  Save it back to Dante now (thus creating a new Lab10 folder there) and save it there again at the end.

  2. Open Visual Basic and load in the project.  Change the caption of the form to Sign Finder 2.

  3. We will need another helper variable, called monthName, of String data type.  Add a declaration for this variable at the top of the Code window, under (General) and (Declarations).  It should follow the variable declarations that are already there.


Simple Tables:

 

In this lab we will improve our application by teaching it about the number of days in each month.  For example, there are 31 days in January, 28 days in Feburary, etc.  How would you represent this information?  A simple way to do so is to write a table like this:

 

 

Key:month

Value:numdays

 

January

31

 

February

28

 

March

31

 

April

30

 

May

31

 

June

30

 

July

31

 

August

31

 

September

30

 

October

31

 

November

30

 

December

31

 

The left column contains keys: the key determines which line of the table we are interested in.  The right column contains values: this is the information we are looking for when given a specific key.  For example, if we want to know the number of days in June, then June is our key.  First we find the line in the table whose Key is June.  Then we look in the Values column on that line to find the desired value: 30. 

 

In this lab, we will keep our life simple.  For example, we will deal with just one column for values.  We will treat all Februaries as having 28 days.  [PS: Different authors and programming environments use a variety of words for what we here call a table, and sometimes use the word table in other ways!]

 

Footnote: A table like this is our first example of a “data structure”: a way of grouping and organizing data.  Data structures form an extremely important part of information technology, both from the theoretical and the practical standpoints.

 

The lifecycle of a table in Visual Basic consists of the following stages:

 

We will go through each of these stages in turn.

 

Creating a table

To create a table, you simply need to declare a variable for that table in a particular way.  Let’s call our table variable daysInMonth.  Then the declaration will look like this:

 

Option Explicit

Dim daysInMonth As New Collection

 

The phrase “As New Collection” hints at the fact that a new object of Collection class is being created.  We will use the Collection class provided by Visual Basic to implement our table.

 

  1. Add this declaration following the existing variable declarations at the top of the Code window, under (General) and (Declarations).
     

Initializing the table

To initialize the table you need to add to it all the lines that you want to be there.  Remember that each line consists of the key and the value.  In general, a line is added to the table daysInMonth with the following code:

 

daysInMonth.Add <value>, <key>

 

*** Remember that the key appears last, and the value first. ***

 

Querying the table

After all lines have been added to the table, you can query it by supplying the key you are interested in and asking for the corresponding value.  In general, the following code queries the table daysInMonth:

 

daysInMonth.Item(<key>)

 

To use the result, assign it to a variable or use it in a conditional.  For example:

 

daysInSeptember = daysInMonth.Item(“September”)

 

 

Now you will start initializing the table daysInMonth to indicate the correct number of days in a month.  (You will query it later in this lab.)  Here is one way of doing so.  When the user clicks on a month option button, the line for that month will be added to the table.  As a result, when the user clicks on the OK button, the table will already be initialized for the chosen month.

 

  1. Go to the code that handles the click event for the January button.  Add code to do the following:

·         assign the value “January” to the variable monthName (we will need this later);

·         add a line to daysInMonth with key “January” and value 31 (the number of days in January)

 

Notice: we haven’t given you the actual VB code that is needed.  Test your understanding by working it out yourself, and writing it out on a piece of paper before trying it on the computer.  Check it with someone, perhaps the person sitting next to you, before typing it in.  (This technique has a name, by the way: “Peer code review.”  It is widely used in industry.  Of course, on a project for credit this might not be appropriate!)

 

So that’s January.  Do the same for February and March.

 

Doesn’t this sound like repetition?  Case for a procedure!  (But first, save your work on disk!)


Procedures:

 

The actions you performed above are a repetition of a general pattern.  You will write a procedure to express this pattern.  But let’s start with some planning.

 

First, decide what the procedure will do: assign the name of the month to the variable monthName, and add a line to the daysInMonth table with that name as the key and the corresponding number of days as the value.  Then, identify the 4 specifications of the procedure.  What are they?

 

Name: a way to refer to the procedure.  Make the name something descriptive of what the procedure does.

 

Parameters: the input and/or output variables used for this procedure (in our case there are no output variables):

 

(monthParameter As String, numDays As Integer)

 

Definition: the instructions or procedure body, performing the actions that we decided upon.  For example:

 

monthName = monthParameter 

daysInMonth.Add numDays, monthName

 

Declaration: put together the entire package: the procedure’s name, parameters and definition are placed in the declaration.  In Visual Basic it is written like this:

 

Private Sub ProcedureName (monthParameter As String, numDays As Integer)

 

          monthName = monthParameter 

          daysInMonth.Add numDays, monthName

 

End Sub

 

 

  1. Plan your own procedure similar to the description above.  Choose concise and meaningful names for the procedure and its parameters.

 

Write the procedure declaration at the bottom of the Code window.  When you need to move to that area later, select General from the Objects drop-down menu and find the name of the procedure in the Procedure drop-down menu.

 

Calling a procedure

Recall that in order to call (or invoke) a procedure, you need to specify its name and actual parameters.  You could write a procedure call in Visual Basic as follows, assuming it has two parameters:

 

ProcedureName <first actual>, <second actual>

 

For the procedure that you just wrote, what are the actuals going to be?

 


Resume initialization of the daysInMonth table

 

Let’s look back at what you have done.  You started to initialize the table by adding lines for each month.  Then you noticed that it was a rather repetitive business, so you wrote a procedure to make it easier.  Now you should go back and complete table initialization – this time by invoking your procedure.

 

  1. Go to the code that handles the click event for the April button.  Add code to invoke your procedure with appropriate actual parameters.  Repeat the same for the remaining months.

 

  1. Now is good time to save your project again, before you start debugging it.  Continue saving it periodically, once you get the next addition to it to work.

 

  1. Run your program; click on a couple of month buttons.  Then interrupt it by clicking on the Pause button.  Now check the values of your variables.  As you did before, go to the top of the Code window, move your mouse over the variables and verify their values.  What should be the value of the monthName variable?  Ensure that monthName actually has that value.

 

This trick does not work for daysInMonth.  Instead, right-click on it and choose Add Watch; just click OK in the menu that pops up.  The Watch window should occur at the bottom of Visual Basic with this variable in there.  Click on the plus sign and you should see which values have been stored in it.  (By the way, what should be values in this case?)

 

Note: the Watch window does not show you the keys, it only shows values.  Also, do not be concerned with the order in which the values appear – the window shows them in the order they were added.

 

  1. Did you remember to save your project now?


Finally, improve the check for the number of days

 

Remember where you check that the number of days the user entered is correct?  Why this check is less than perfect?

 

  1. In the Code window go to the code that handles clicks on the OK button.  Instead of using the number 31 as the maximum allowed number of days in the conditional, insert a query to the daysInMonth table.  The key for the lookup is the name of the month that the user selected.  It is stored in the variable monthName.

  2. Run your code again.  Check it for entering reasonable and unreasonable dates.  Does it work correctly?

  3. Improve the message that is printed to the user in case the date is unreasonable.  For that, create another label, say WrongDay:, to handle this particular case, as follows.  Leave the ErrorHandler: label to handle other errors.

 

    1. At the bottom of this sub, insert the following code (shown in bold):

 

WrongDay:

          MsgBox  <you will insert your message here>

Exit Sub

 

ErrorHandler:

          MsgBox "Please enter a valid date and check a month", vbOKOnly

          txtUserInput.SetFocus

End Sub

 

    1. In the line with the conditional that checks correctness of the day, replace “Then GoTo ErrorHandler” with “Then GoTo WrongDay”.

    2. Finally, compose the message that the MsgBox will display.  It should say something like “The day you entered is incorrect.  There are only 30 days in April.” adjusting, of course, to the month that the user selected.

  1. Run your program again.  Try to enter information that will “break” it.  Can you do it?


Creating an Executable File:

 

  1. Make SignFinder executable when you are finished and happy with it.  Save your project and FTP your Lab10 folder to your Dante account.



Supplementary Questions

 

       I.      You want to create a table, say monthNames, that holds the names of the months (i.e., the corresponding strings).  You want to look up the names using numbers, from 1 to 12.  For example, when you query the table with the number 5, you get “May”.  Write VB code to:

a.      create the table,

b.      add the line for May to it,

c.      query it for that line.

     II.      Suggest a procedure that does some of the above.  What parameters would it have?


    III.      A table is an example of a “data structure:” a way of organizing data.  Despite their seeming simplicity, tables are an important data structuring concept and can be very powerful in programming.

a.      How could you view a telephone book as a table?  What are the keys and values?

b.      Same question for Webster’s Dictionary.

c.      Think of another real-world example of a table, and say what its keys and values are.