Download presentation
Presentation is loading. Please wait.
Published byAlannah Barton Modified over 9 years ago
1
Chapter 10: Structures and Sequential Access Files
2
Previewing the CD Collection Application
Keeps track of a person’s CD collection Saves each CD’s name, artist’s name, and price Uses a sequential access file named CDs.txt Can add information to or remove information from a file Figure 10-1 CD information added to the list box Figure 10-2 Contents of the CDs.txt file Programming with Microsoft Visual Basic 2012
3
Lesson A Objectives After studying Lesson A, 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 Programming with Microsoft Visual Basic 2012
4
Structures Structure statement Structure (or user-defined data type)
Enables you to create your own data types Used to group related items of different data types into one unit Typically appears in a form’s Declaration section Structure (or user-defined data type) A data type created with the Structure statement Member variables Variables, constants, or procedures declared within the structure declaration Programming with Microsoft Visual Basic 2012
5
Structures (cont.) Programming with Microsoft Visual Basic 2012
Figure 10-3 Syntax and an example of the Structure statement Programming with Microsoft Visual Basic 2012
6
Declaring and Using a Structure Variable
Structure variables Declared using the structure data type Example: Dim hourly As Employee hourly is a variable declared with the Employee structure type To access the member variable in code, use structureVariableName.memberVariableName Example: hourly.dblPay = 26 Member variables are used like scalar variables Programming with Microsoft Visual Basic 2012
7
Declaring and Using a Structure Variable (cont.)
Figure 10-4 Syntax and examples of declaring a structure variable Figure 10-5 Examples of using a member variable Programming with Microsoft Visual Basic 2012
8
Passing a Structure Variable to a Procedure
Application for the sales manager at Norbert Pool & Spa Depot Allows the user to enter the length, width, and depth Calculates the gallons of water needed to fill a pool Advantages of using a structure to group dimensions: Three inputs are stored in one structure variable You pass a single structure variable to a procedure instead of three scalar variables Your code is structured in a more readable form Programming with Microsoft Visual Basic 2012
9
Passing a Structure Variable to a Procedure (cont.)
Figure 10-6 Interface showing the required number of gallons Figure 10-7 Code for the Norbert Pool & Spa Depot application (without a structure) Programming with Microsoft Visual Basic 2012
10
Passing a Structure Variable to a Procedure (cont.)
Figure 10-8 Code for the Norbert Pool & Spa Depot application (with a structure) Programming with Microsoft Visual Basic 2012
11
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) A one-dimensional array of structure variables Each structure variable will contain: A String variable for the ID An Integer variable for the price Programming with Microsoft Visual Basic 2012
12
Creating an Array of Structure Variables (cont.)
Figure 10-9 Problem specification for the Treasures Gift Shoppe Figure Interface for the Treasures Gift Shoppe application Programming with Microsoft Visual Basic 2012
13
Creating an Array of Structure Variables (cont.)
Figure Code for the Treasures Gift Shoppe application (without a structure) Programming with Microsoft Visual Basic 2012
14
Creating an Array of Structure Variables (cont.)
Figure Syntax and examples of referring to member variables in an array Programming with Microsoft Visual Basic 2012
15
Creating an Array of Structure Variables (cont.)
Figure Code for the Treasures Gift Shoppe application (with a structure) Figure Interface showing the price for product ID FE15 Programming with Microsoft Visual Basic 2012
16
Lesson A Summary To create a structure (user-defined data type):
Use the Structure statement In most applications, you enter the Structure statement in the form’s Declarations section To declare a structure variable, use the following syntax: {Dim | Private} structureVariableName As structureName Programming with Microsoft Visual Basic 2012
17
Lesson A Summary (cont.)
To refer to a member within a structure variable, use the following syntax: structureVariableName.memberVariableName To create an array of structure variables: Declare the array using the structure as the data type To refer to a member within a structure variable stored in an array, use the following syntax: ArrayName(subscript).memberVariableName Programming with Microsoft Visual Basic 2012
18
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 2012
19
Sequential Access Files
Reading a file means getting data from a file Writing to a file means sending data to a file Output files store application output An application uses data in input files Sequential access files Composed of lines of text that are both read and written in consecutive order Also called text files Programming with Microsoft Visual Basic 2012
20
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 Must declare the StreamWriter variable The Game Show Contestants application uses the StreamWriter variable Programming with Microsoft Visual Basic 2012
21
Writing Data to a Sequential Access File (cont.)
Figure Syntax and an example of declaring a StreamWriter variable Figure Interface for the Game Show Contestants application Programming with Microsoft Visual Basic 2012
22
Writing Data to a Sequential Access File (cont.)
CreateText method Used to open a new sequential file AppendText method Used to open a file that exists and add data to it Figure Syntax and examples of the CreateText and AppendText methods Programming with Microsoft Visual Basic 2012
23
Writing Data to a Sequential Access File (cont.)
Write method Leaves the cursor at the end of the last character WriteLine method Moves the cursor to the next line Figure Syntax and examples of the Write and WriteLine methods Programming with Microsoft Visual Basic 2012
24
Closing an Output Sequential Access File
Close method Used to close an output sequential access file All open files must be closed before being opened again Figure Syntax and an example of closing an output sequential access file Programming with Microsoft Visual Basic 2012
25
Reading Data from a Sequential Access File
StreamReader object Used to read data from a sequential access file Must declare the StreamReader variable OpenText method Used to open a sequential access file for input You can use this method to automatically create a StreamReader object Exists method Used to determine if a file exists Returns True if a file exists, otherwise False Programming with Microsoft Visual Basic 2012
26
Reading Data from a Sequential Access File (cont.)
Figure Syntax and an example of declaring a StreamReader variable Figure Syntax and an example of the OpenText method Figure Syntax and an example of the Exists method Programming with Microsoft Visual Basic 2012
27
Reading Data from a Sequential Access File (cont.)
Figure Additional code entered in the procedure Programming with Microsoft Visual Basic 2012
28
Reading Data from a Sequential Access File (cont.)
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 Returns the String value containing data in the current line Returns only data, not including the newline character Peek method Determines whether a file contains another character to read Programming with Microsoft Visual Basic 2012
29
Reading Data from a Sequential Access File (cont.)
Figure Syntax and an example of the ReadLine method Figure Syntax and an example of the Peek method Programming with Microsoft Visual Basic 2012
30
Closing an Input Sequential Access File
Close method Used to close input sequential access files Figure Syntax and an example of closing an input sequential access file Programming with Microsoft Visual Basic 2012
31
Closing an Input Sequential Access File (cont.)
Figure Click event procedures for the btnWrite and btnRead controls (continues) Programming with Microsoft Visual Basic 2012
32
Closing an Input Sequential Access File (cont.)
(continued) Figure Click event procedures for the btnWrite and btnRead controls Programming with Microsoft Visual Basic 2012
33
Closing an Input Sequential Access File (cont.)
Figure Five contestant names listed in the Contestants box Figure Nine contestant names listed in the list box Programming with Microsoft Visual Basic 2012
34
Lesson B Summary To write data to a sequential access file:
Declare a StreamWriter variable and then use either the CreateText method or the AppendText method to open a sequential access file Assign the method’s return value to the StreamWriter variable Use either the Write method or the WriteLine method to write the data to the file Close the file using the Close method Programming with Microsoft Visual Basic 2012
35
Lesson B Summary (cont.)
To read data from a sequential access file: Declare a StreamReader variable Use the Exists method to determine whether the sequential access file exists If the file exists, use the OpenText method to open the file Assign the method’s return value to the StreamReader variable Use the ReadLine and Peek methods to read the data from the file Close the file using the Close method Programming with Microsoft Visual Basic 2012
36
Lesson B Summary (cont.)
To determine whether a sequential access file exists: Use the Exists method using the syntax IO.File.Exists(fileName) The method returns the Boolean value True if the file exists; otherwise, it returns the Boolean value False To determine whether the end of a sequential access file has been reached: Use the Peek method using the syntax streamReaderVariableName.Peek The method returns the number –1 when the end of the file has been reached; otherwise, it returns the next character in the file Programming with Microsoft Visual Basic 2012
37
Lesson C Objectives After studying Lesson C, you should be able to:
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 Write records to a sequential access file Programming with Microsoft Visual Basic 2012
38
Coding the CD Collection Application
Figure Interface for the CD Collection application Figure TOE chart for the CD Collection application Figure CDs.txt window Programming with Microsoft Visual Basic 2012
39
Coding the Form’s Load Event Procedure
Figure Pseudocode for the form’s Load event procedure Figure Additional comment and code entered in the Load event procedure Programming with Microsoft Visual Basic 2012
40
Coding the Form’s Load Event Procedure (cont.)
Figure Contents of the CDs.txt file shown in the list box Programming with Microsoft Visual Basic 2012
41
Coding the btnAdd_Click Procedure
Figure Pseudocode for the btnAdd_Click procedure Programming with Microsoft Visual Basic 2012
42
Aligning Columns of Information
PadLeft and PadRight methods Used to pad strings with characters These methods can be used to align text in a list box or text written to a sequential access file Strings.Space method Used to include a specific number of space characters in a string Syntax: Strings.Space(number) number is an integer representing the number of spaces to include Programming with Microsoft Visual Basic 2012
43
Aligning Columns of Information (cont.)
Figure Examples of aligning columns of information Programming with Microsoft Visual Basic 2012
44
Aligning Columns of Information (cont.)
Figure CD information added to the list box Programming with Microsoft Visual Basic 2012
45
Coding the btnRemove_Click Procedure
Figure Pseudocode for the btnRemove_Click procedure Figure Syntax and examples of the Items collection’s Remove and RemoveAt methods Programming with Microsoft Visual Basic 2012
46
Coding the Form’s FormClosing Event Procedure
Figure Pseudocode for the form’s FormClosing event procedure Figure CD information saved in the CDs.txt file Figure Current contents of the CDs.txt file Programming with Microsoft Visual Basic 2012
47
Coding the Form’s FormClosing Event Procedure (cont.)
Figure Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012
48
Coding the Form’s FormClosing Event Procedure (cont.)
(continued) Figure Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012
49
Coding the Form’s FormClosing Event Procedure (cont.)
(continued) Figure Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012
50
Coding the Form’s FormClosing Event Procedure (cont.)
(continued) Figure Code for the CD Collection application Programming with Microsoft Visual Basic 2012
51
Lesson C Summary To align columns of information:
Use the PadLeft and PadRight methods To align a column of numbers by the decimal point: Format each number in the column to ensure that each has the same number of digits to the right of the decimal point, and then use the PadLeft method to right-align the numbers To include a specific number of spaces in a string: Use the Strings.Space method using the syntax Strings.Space(number) in which number is an integer that represents the number of spaces to include Programming with Microsoft Visual Basic 2012
52
Lesson C Summary To remove an item from a list box:
Use either the Items collection’s Remove method or its RemoveAt method The Remove method’s syntax is object.Items.Remove(item) where item is the value of the item you want to remove The RemoveAt method’s syntax is object.Items.RemoveAt(index) where index is the index of the item you want removed Programming with Microsoft Visual Basic 2012
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.