programming with microsoft visual basic 2008 fourth edition chapter ten structures and sequential...
DESCRIPTION
Previewing the CD Collection Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition3 Figure 10-1: CD information added to the list boxTRANSCRIPT
Programming with Microsoft Visual Basic 2008
Fourth Edition
Chapter TenStructures and Sequential Access Files
Previewing the CD Collection Application
• CD Collection Application: Keeps track of person’s CD collection– Saves each CD’s name, artist’s name, and price– Uses sequential access file named CDs.txt– Can add to or remove information from file
• Open the CD.exe file
Programming with Microsoft Visual Basic 2008, Fourth Edition 2
Previewing the CD Collection Application (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 3
Figure 10-1: CD information added to the list box
Previewing the CD Collection Application (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 4
Figure 10-2: Contents of the CDs.txt file
Lesson A Objectives
After studying Lesson A, you should be able to:• Create a structure• Declare a structure variable• Pass a structure variable to a procedure• Create and manipulate a one-dimensional array
of structures
Programming with Microsoft Visual Basic 2008, Fourth Edition 5
Creating a Structure
• Structure statement:– Enables you to create your own data types– Used to group related items of different data types
into one unit– Typically appears in form’s Declaration section
• Structure (or user-defined data type): Data type created with Structure statement
• Member variables:– Variables, constants, or procedures declared
within structure declaration
Programming with Microsoft Visual Basic 2008, Fourth Edition 6
Creating a Structure (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 7
Figure 10-3: Syntax and an example of the Structure statement
Declaring and Using a Structure Variable
• Structure variables: Declared using structure– Structure is data type for variable
• Example: Dim manager As Employee – manager is variable declared with Employee
structure type • Accessing member variable in code:
– Use structureVariableName. memberName– Example: manager.dblSalary = 59000D
• Member variables are used like scalar variables
Programming with Microsoft Visual Basic 2008, Fourth Edition 8
Declaring and Using a Structure Variable (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 9
Figure 10-4: Syntax and examples of declaring a structure variable
Declaring and Using a Structure Variable (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 10
Figure 10-5: Examples of using a member variable
Passing a Structure Variable to a Procedure
• Application for sales manager at Willows Pool:– Allows user to enter length, width, and depth– Calculates volume of pool
• Advantages of using structure to group dimensions:– Three inputs are stored in one structure variable– You pass single structure variable to procedure
instead of three scalar variables– Your code is structured in more readable form
Programming with Microsoft Visual Basic 2008, Fourth Edition 11
Programming with Microsoft Visual Basic 2008, Fourth Edition 12
Figure 10-7: Code for the Willow Pools application (without a structure)
Passing a Structure Variable to a Procedure (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 13
Figure 10-8: Code for the Willow Pools application (with a structure)
Passing a Structure Variable to a Procedure (continued)
Creating an Array of Structure Variables
• Three ways to manage pairs of ID-price data:– Two parallel one-dimensional arrays– One two-dimensional array (tabular format)– One-dimensional array of structure variables
• Structure variable will contain:– String variable for ID– Integer variable for price
Programming with Microsoft Visual Basic 2008, Fourth Edition 14
Programming with Microsoft Visual Basic 2008, Fourth Edition 15
Figure 10-9: Code for the Treasures Gift Shop application (without a structure)
Creating an Array of Structure Variables (continued)
Creating an Array of Structure Variables (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 16
Figure 10-10: Names of some of the member variables in the priceList array
Lesson A Summary
• Structures are user-defined data types• Structure members can be variables, constants,
or procedures• Refer to member within structure variable using
structureVariableName.memberName• Element in array of structure variables is
structure variable • Refer to member within structure variable stored
in an array using: arrayName(subscript).memberName
Programming with Microsoft Visual Basic 2008, Fourth Edition 17
Lesson B Objectives
After studying Lesson B, you should be able to:• Open and close a sequential access file• Write data to a sequential access file• Read data from a sequential access file• Determine whether a sequential access file
exists• Test for the end of a sequential access file
Programming with Microsoft Visual Basic 2008, Fourth Edition 18
Sequential Access Files
• Reading a file: Getting data from a file• Writing to a file: Sending data to a file• Output files: Files to which information is written• Input files: Files that are read by a computer• Sequential access files: Files composed of
lines of text that are both read and written sequentially
Programming with Microsoft Visual Basic 2008, Fourth Edition 19
Writing Data to a Sequential Access File
• Stream of characters: Sequence of characters• StreamWriter object: Used to write stream of
characters to sequential access file– Must declare StreamWriter variable
• Game Show Contestants sample application uses StreamWriter variable
Programming with Microsoft Visual Basic 2008, Fourth Edition 20
Writing Data to a Sequential Access File (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 21
Figure 10-15: Syntax and an example of declaring a StreamWriter variable
Writing Data to a Sequential Access File (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 22
Figure 10-17: Syntax and examples of creating a StreamWriter object by opening a file
Writing Data to a Sequential Access File (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 23
Figure 10-18: Syntax and examples of writing data to a sequential access file
Writing Data to a Sequential Access File (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 24
Figure 10-19: Names contained in the contestants.txt file
Reading Data From a Sequential Access File
• StreamReader object: Used to read data from sequential access file– Must declare StreamReader variable
• OpenText method: Used to open sequential access file for input– Can use this method to automatically create
StreamReader object• Exists method: Used to determine if file exists
– Returns True if file exists, otherwise False
Programming with Microsoft Visual Basic 2008, Fourth Edition 25
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 26
Figure 10-21: Syntax and an example of creating a StreamReader object by opening a file
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 27
Figure 10-22: Syntax and an example of the Exists method
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 28
Figure 10-23: Code entered in the selection structure’s false path
Reading Data From a Sequential Access File
(continued)• Line: Sequence (stream) of characters followed
by newline character• ReadLine method: Used to read contents of file,
one line at a time– Returns String value containing data in current line– Returns only data, not including newline character
• Peek method: Determines whether file contains another character to read
Programming with Microsoft Visual Basic 2008, Fourth Edition 29
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 30
Figure 10-24: Syntax and examples of reading data from a sequential access file
Programming with Microsoft Visual Basic 2008, Fourth Edition 31
Figure 10-25: Code entered in the btnWrite and btnRead controls’ Click event procedures
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 32
Figure 10-25: Code entered in the btnWrite and btnRead controls’ Click event procedures (continued)
Reading Data From a Sequential Access File
(continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 33
Figure 10-26: Contestant names appear in the Contestants box
Reading Data From a Sequential Access File
(continued)
Lesson B Summary
• Sequential access file: Stores data items in consecutive order (sequentially)
• Use StreamWriter variable to write data to sequential access file
• Use StreamReader variable to read data from sequential access file
• Use Exists method to determine if file exists• Use Peek method to determine whether end of
sequential access file has been reached
Programming with Microsoft Visual Basic 2008, Fourth Edition 34
Lesson C Objectives
After studying Lesson C, you should be able to:• Fill a list box with values stored in a sequential
access file• Add an item to a list box while an application is
running• Align columns of information• Remove an item from a list box while an
application is running• Save list box items in a sequential access file
Programming with Microsoft Visual Basic 2008, Fourth Edition 35
Coding the CD Collection Application
Programming with Microsoft Visual Basic 2008, Fourth Edition 36
Figure 10-28: User interface for the CD Collection application
Coding the CD Collection Application (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 37
Figure 10-29: TOE chart for the CD Collection application
Coding the CD Collection Application (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 38
Figure 10-30: CDs.txt window showing the contents of the sequential access file
Coding the Form’s Load Event Procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition 39
Figure 10-31: Pseudocode for the form’s Load event procedure
Coding the Form’s Load Event Procedure (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 40
Figure 10-32: Additional comment and code entered in the form’s Load event procedure
Coding the Form’s Load Event Procedure (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 41
Figure 10-33: The contents of the CDs.txt file appear in the list box
Coding the btnAdd Control’s Click Event Procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition 42
Figure 10-34: Pseudocode for the btnAdd control’s Click event procedure
43Programming with Microsoft Visual Basic 2005, Third Edition
Aligning Columns of Information
• PadLeft and PadRight methods: Used to pad strings with characters – These methods can be used to align text in list box or
text written to sequential access file• Strings.Space method: Used to include specific
number of space characters in string• Syntax: Strings.Space(number)
– number: Integer representing number of spaces to include
Coding the btnRemove Control’s Click Event Procedure
• Main task: Allow user to remove selected line from list box control
• RemoveAt method: Removes list box item at specified index
Programming with Microsoft Visual Basic 2008, Fourth Edition 44
Coding the btnRemove Control’s Click Event Procedure (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition 45
Figure 10-37: Pseudocode for the btnRemove control’s Click event procedure
Coding the Form’s FormClosing Event Procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition 46
Figure 10-38: Pseudocode for the form’s FormClosing event procedure
Lesson C Summary
• Use Add method of list box to add an item to list• Use PadLeft method to right-align string, or to
align column of numbers by decimal point• Use PadRight method to left-align string• Use Strings.Space method to include specific
number of spaces in string• Use RemoveAt method to remove item at
specific index from listbox
Programming with Microsoft Visual Basic 2008, Fourth Edition 47