FIT 100
Lab Activity 10:
�What�s Your Sign?� Made Smarter
Winter� 2002
Recommended Reading for
Lab 10:
�
Chapter 13 from the FIT book
�
VB book: Ch.6
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.
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.
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.
� 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
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.
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.
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?
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
Creating an Executable File:
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.