Fourth Edition Chapter Eleven Structures and Sequential Files

Microsoft Visual Basic 2010:
Reloaded
Fourth Edition
Chapter Eleven
Structures and Sequential Files
Objectives
After studying this chapter, you should be able to:
• Create a structure
• Declare and use a structure variable
• Pass a structure variable to a procedure
• Create an array of structure variables
• Write data to a sequential access file
• Close a sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
2
Objectives (cont'd.)
•
•
•
•
•
•
Read data from a sequential access file
Use the Exists and Peek methods
Align columns of information
Code the FormClosing event procedure
Write and read records
Use the Split function
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
3
Structures
• Structure statement: used to create your own data
type
• User-defined data types (or structures): data
types created by using the Structure statement
• Member variables: variables defined within a
structure
• Structure can include member variables of:
– Any standard data types
– Other structure types
– Used to group related items into one unit
• Usually declared in the form’s Declarations section
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
4
Structures (cont'd.)
Figure 11-1: How to define a structure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
5
Declaring a Structure Variable
• Structure statement only creates the data type
• Structure variables: variables declared using a
structure
Figure 11-2: How to declare a structure variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
6
Declaring a Structure Variable (cont'd.)
• Refer to an entire structure in code using its name
• Refer to a member variable using the structure
name with the dot operator and the member
variable name
• Member variables can be used like any other
variables
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
7
Declaring a Structure Variable (cont'd.)
Figure 11-3: How to use a member variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
8
Passing a Structure Variable
to a Procedure
• Sample application without using a structure:
Figure 11-4: Sample run of the Willow Pools application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
9
Figure 11-5: Code
for the Willow Pools
application (without
a structure)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
10
Figure 11-6: Code for the Willow Pools application (with a structure)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
11
Passing a Structure Variable to a
Procedure (cont'd.)
• When you pass a structure variable to a procedure,
all of its member variables are automatically
passed
• Sample application using a structure:
– Uses less code to pass a structure variable to a
procedure
– Stores all of the data in a single unit
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
12
Creating an Array
of Structure Variables
• Use a one-dimensional array of structure variables
– Each element in the array is a structure variable
Figure 11-7: Sample run of the Treasures Gift Shop application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
13
Figure 11-8: Partial code for the Treasures Gift Shop application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
14
Creating an Array
of Structure Variables (cont’d.)
Figure 11-8: Partial code for the Treasures Gift Shop application (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
15
File Types
•
•
•
•
•
Reading a file: getting information from a file
Writing to a file: sending information to a file
Output files: files to which information is written
Input files: files that are read by the computer
Sequential access files (text files): composed of
text that are both read and written sequentially, one
line at a time
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
16
Writing Data to a
Sequential Access File
• Stream of characters: a sequence of characters
• StreamWriter object: used to write a stream of
characters to a sequential access file
• Use IO.File methods to manipulate the file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
17
Writing Data to a
Sequential Access File (cont'd.)
Figure 11-9: How to declare a StreamWriter variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
18
Writing Data to a
Sequential Access File (cont'd.)
• CreateText method: opens a sequential access
file for output
– Creates a new, empty file to which data can be
written
– If the named file already exists, its contents are
erased before writing to it
• AppendText method: opens a sequential access
file for append
– New data is written after any existing data in the file
– If the named file does not exist, it is created for you
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
19
Figure 11-10: How to create a StreamWriter object
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
20
Writing Data to a
Sequential Access File (cont'd.)
• After opening a file for output or append, you can
begin writing data to it
• Write method: writes a line of data to the file
• WriteLine method: writes a line of data to the file
and writes a newline character after the data
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
21
Writing Data to a
Sequential Access File (cont'd.)
Figure 11-11: How to write to a sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
22
Closing an Output
Sequential Access File
• Close method: used to close an output sequential
access file
– Ensures that the data is saved
– Makes the file available for use elsewhere in the
application
– Uses a StreamWriter variable to refer to the file in
code
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
23
Closing an Output
Sequential Access File (cont’d.)
Figure 11-12: How to close an output sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
24
Reading Data from a
Sequential Access File
• StreamReader object: used to read data from a
sequential access file
• Must first declare a StreamReader variable
• OpenText method: used to open a sequential
access file for input
– Automatically creates a StreamReader object
• Arguments:
– fileName: the filename and optionally the path to the
file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
25
Reading Data from a
Sequential Access File (cont'd.)
Figure 11-13: How to declare a StreamReader variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
26
Reading Data from a
Sequential Access File (cont'd.)
Figure 11-14: How to create a StreamReader object
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
27
Reading Data from a
Sequential Access File (cont'd.)
• If an input file cannot be located at run time, an
error occurs
• You can avoid the error by determining if the file
exists before trying to open it
• Exists method: returns a Boolean value of True if
a file exists; otherwise returns a Boolean value of
False
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
28
Reading Data from a
Sequential Access File (cont'd.)
Figure 11-15: How to determine whether a sequential access file exists
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
29
Reading Data from a
Sequential Access File (cont'd.)
• Line: a sequence (stream) of characters followed
by the newline character
• ReadLine method: used to read the contents of a
file, one line at a time
– Does not include the newline character at the end of
the line
– Uses the file associated with the StreamReader
variable
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
30
Reading Data from a
Sequential Access File (cont'd.)
Figure 11-16: How to read data from a sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
31
Reading Data from a
Sequential Access File (cont'd.)
• Peek method: “peeks” into the file to determine
whether the file contains another character to read
– If there is another character, Peek returns the
character
– If there are no more characters, Peek returns a
value of -1
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
32
Reading Data from a
Sequential Access File (cont'd.)
Figure 11-17: How to use the Peek method
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
33
Closing an
Input Sequential Access File
• Use the Close method to close an input sequential
access file when you are finished using it
– Makes the file available for use elsewhere in the
application
Figure 11-18: How to close an input sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
34
The Game Show Contestants
Application
• Sample application: used to record the names of
game show contestants in a sequential access file
Figure 11-19: Sample run of the Game Show Contestants application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
35
Figure 11-20: Partial
code for the Game Show
Contestants application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
36
The Game Show Contestants
Application (cont’d.)
Figure 11-20: Partial code for the Game Show Contestants application (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
37
Aligning Columns of Information
• PadLeft, PadRight methods:
– Pads a string with a specified number of characters
based on current length of the string being padded
– Used to align columns of information written to a
sequential access file
– The default pad character is the space character
• Align columns of numeric information by the
decimal point
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
38
Figure 11-22:
How to align
columns of
information
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
39
Aligning Columns of Information
(cont'd.)
Figure 11-22: How to align columns of information (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
40
The FormClosing Event
• FormClosing event:
– Occurs when a form is about to be closed by the
program code or by the user
– Allows you to trap the closing action and take any
necessary actions such as saving data
– Can be used to cancel the close action
– Set e.Cancel = True to cancel the closing action
• Form may be closed by Me.Close() statement or by
user clicking the Close button on title bar
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
41
The FormClosing Event (cont'd.)
Figure 11-23: How to use the FormClosing event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
42
The FormClosing Event (cont'd.)
Figure 11-23: How to use the FormClosing event procedure (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
43
Writing and Reading Records
• Field: a single item of information about a person,
place, or thing
• Record: a group of related fields that contain all of
the necessary data about a specific person, place,
or thing
• If a record contains more than one field, the fields
are separated by a delimiter character
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
44
Figure 11-24: How to write records to a sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
45
Writing and Reading Records (cont’d.)
• Split function: used with ReadLine to divide a
line of text from a file into fields
– Assigns each field to an element in an array
– Must specify the delimiter that was used in the file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
46
Writing and Reading Records (cont’d.)
Figure 11-25: How to read records from a sequential access file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
47
Writing and Reading Records (cont’d.)
Figure 11-25: How to read records from a sequential access file (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
48
Programming Tutorial 1
• Modifying the Concentration Game Application
– Use Concentration Game application from Ch. 8
Figure 11-26: Contents of three of the four sequential access files
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
49
Programming Tutorial 2
• Creating the CD Collection Application
Figure 11-30: MainForm for the CD Collection application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
50
Programming Example
• Glovers Industries Application:
– Display items and prices that are stored in a
sequential access file
Figure 11-40: MainForm in the Glovers Industries application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
51
Summary
• Structure statement: defines a user-defined data
type or structure
• Structure variable contains one or more member
variables
• Structure variable can be passed to procedures
• Can create a one-dimensional array of structure
variables
• Use the dot member access operator to access the
structure member variables
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
52
Summary (cont'd.)
• Application can read from and write to a file
• Sequential access files are always accessed in
consecutive (sequential) order from beginning to
end
• StreamWriter variable: used to write data to a
sequential access file
• StreamReader variable: used to read data from a
sequential access file
• Close method: closes a file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
53
Summary (cont'd.)
• Use PadLeft and PadRight methods to align
data in a sequential access file
• FormClosing event: occurs when the form is
about to be closed
• Fields in a record are separated by a delimiter
character
• Use the Split function to read delimited records
from a file
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
54