Lab 11: “What’s Your Sign?” with Tables and
Procedures
1. Open Project and Declare a New
Variable
6. Resume initialization of the
daysInMonth table
7. Improve the check for the number of
days
8. Creating an Executable File:
Computer Programming
Fundamentals with Applications in Visual Basic 6.0 is on
reserve at Odegaard Undergraduate Library.
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. You will also work with data structures,
specifically tables. In VB tables are
called collections. Collections are a
way to group together similar items that are important to us and to access them
based on an index (number) or a key (string value). Collections are like the arrays you have read about in Chapter
13, but instead of using the index to access a value, a key is used instead.
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 where the 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!] 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. |
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 the Collection class is being created. We will use the Collection class
provided by Visual Basic to implement our table. In VB, there are 4 methods that are available to the
Collections object. Methods are actions
that can be taken by an object.
They are noted in VB syntax just as properties are: the name of the object, a dot, then the
method. What are the names of the 4 methods and what do they
do? and search for Collections
HINT: Use the Object Browser: View>Object Browser
|
Go to the code
that handles the click event for the January button. Add code to do the following:
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. Let’s call it setMonth. Parameters:
the input and/or output variables used for
this procedure (in our case there are no output variables): (month As String, numDays
As Integer) Definition:
the instructions or procedure body, performing the
actions that we decided upon. For
example: monthName =
month daysInMonth.Add
numDays, month 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 setMonth (month As
String, numDays As Integer) monthName = month daysInMonth.Add numDays, month End Sub
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. 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: Call
setMonth (<first
actual>, <second actual>) For the procedure that you just wrote, what are the actual
parameters going to be?
Calling
a Procedure
Remember where your
program checks that the number of days the user enters is correct? How can we improve this check with the new
variables?
a. 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
b. In
the line with the conditional that checks correctness of the day, replace “Then
GoTo ErrorHandler” with “Then GoTo WrongDay”.
c. Finally,
compose the message that the MsgBox will display. It would 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.
·
Look for the sign of a birthday in
September
·
Then look for the sign of a birthday
in March
·
Look for ANOTHER birthday in
September?
·
Create the table,
·
Add the line for May to it,
·
Query it for that line.