52
Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Embed Size (px)

Citation preview

Page 1: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012

Chapter 10: Structures and Sequential Access Files

Page 2: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Previewing the CD Collection Application

Programming with Microsoft Visual Basic 2012 2

Figure 10-1 CD information added to the list box

Figure 10-2 Contents of the CDs.txt file

• 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

Page 3: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 3

Page 4: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

• 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 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 4

Structures

Page 5: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 5

Structures (cont.)

Figure 10-3 Syntax and an example of the Structure statement

Page 6: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 7: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 7

Declaring and Using a Structure Variable (cont.)

Figure 10-5 Examples of using a member variable

Figure 10-4 Syntax and examples of declaring a structure variable

Page 8: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

• 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 8

Passing a Structure Variable to a Procedure

Page 9: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 9

Passing a Structure Variable to a Procedure (cont.)

Figure 10-7 Code for the Norbert Pool & Spa Depot application (without a structure)

Figure 10-6 Interface showing the required number of gallons

Page 10: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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)

Page 11: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

• 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 11

Creating an Array of Structure Variables

Page 12: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 12

Creating an Array of Structure Variables (cont.)

Figure 10-10 Interface for the Treasures Gift Shoppe application

Figure 10-9 Problem specification for the Treasures Gift Shoppe

Page 13: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 13

Creating an Array of Structure Variables (cont.)

Figure 10-11 Code for the Treasures Gift Shoppe application (without a structure)

Page 14: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 14

Creating an Array of Structure Variables (cont.)

Figure 10-12 Syntax and examples of referring to member variables in an array

Page 15: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 15

Creating an Array of Structure Variables (cont.)

Figure 10-13 Code for the Treasures Gift Shoppe application (with a structure)

Figure 10-14 Interface showing the price for product ID FE15

Page 16: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 16

Page 17: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 17

Page 18: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 18

Page 19: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 20: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access 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

Page 21: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 21

Writing Data to a Sequential Access File (cont.)

Figure 10-17 Syntax and an example of declaring a StreamWriter variable

Figure 10-18 Interface for the Game Show Contestants application

Page 22: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 22

Figure 10-19 Syntax and examples of the CreateText and AppendText methods

• CreateText method– Used to open a new sequential file

• AppendText method– Used to open a file that exists and add data to it

Writing Data to a Sequential Access File (cont.)

Page 23: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 23

Figure 10-20 Syntax and examples of the Write and WriteLine methods

• Write method– Leaves the cursor at the end of the last character

• WriteLine method– Moves the cursor to the next line

Writing Data to a Sequential Access File (cont.)

Page 24: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 24

Closing an Output Sequential Access File

Figure 10-21 Syntax and an example of 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

Page 25: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 26: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 26

Reading Data from a Sequential Access File (cont.)

Figure 10-23 Syntax and an example of declaring a StreamReader variable

Figure 10-24 Syntax and an example of the OpenText method

Figure 10-25 Syntax and an example of the Exists method

Page 27: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 27

Figure 10-26 Additional code entered in the procedure

Reading Data from a Sequential Access File (cont.)

Page 28: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 28

• 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

Reading Data from a Sequential Access File (cont.)

Page 29: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 29

Figure 10-27 Syntax and an example of the ReadLine method

Figure 10-28 Syntax and an example of the Peek method

Reading Data from a Sequential Access File (cont.)

Page 30: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

• Close method– Used to close input sequential access files

Programming with Microsoft Visual Basic 2012 30

Closing an Input Sequential Access File

Figure 10-29 Syntax and an example of closing an input sequential access file

Page 31: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 31

Closing an Input Sequential Access File (cont.)

Figure 10-30 Click event procedures for the btnWrite and btnRead controls (continues)

Page 32: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 32

Figure 10-30 Click event procedures for the btnWrite and btnRead controls

Closing an Input Sequential Access File (cont.)

(continued)

Page 33: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 33

Figure 10-32 Nine contestant names listed in the list box

Figure 10-31 Five contestant names listed in the Contestants box

Closing an Input Sequential Access File (cont.)

Page 34: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 35: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 36: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 37: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 37

Page 38: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the CD Collection Application

Programming with Microsoft Visual Basic 2012 38

Figure 10-34 Interface for the CD Collection application

Figure 10-35 TOE chart for the CD Collection application

Figure 10-36 CDs.txt window

Page 39: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the Form’s Load Event Procedure

Programming with Microsoft Visual Basic 2012 39

Figure 10-37 Pseudocode for the form’s Load event procedure

Figure 10-38 Additional comment and code entered in the Load event procedure

Page 40: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the Form’s Load Event Procedure (cont.)

Programming with Microsoft Visual Basic 2012 40

Figure 10-39 Contents of the CDs.txt file shown in the list box

Page 41: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the btnAdd_Click Procedure

Programming with Microsoft Visual Basic 2012 41

Figure 10-40 Pseudocode for the btnAdd_Click procedure

Page 42: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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 42

Page 43: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Aligning Columns of Information (cont.)

Programming with Microsoft Visual Basic 2012 43

Figure 10-41 Examples of aligning columns of information

Page 44: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Aligning Columns of Information (cont.)

Programming with Microsoft Visual Basic 2012 44

Figure 10-42 CD information added to the list box

Page 45: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the btnRemove_Click Procedure

Programming with Microsoft Visual Basic 2012 45

Figure 10-43 Pseudocode for the btnRemove_Click procedure

Figure 10-44 Syntax and examples of the Items collection’s Remove and RemoveAt methods

Page 46: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the Form’s FormClosing Event Procedure

Programming with Microsoft Visual Basic 2012 46

Figure 10-45 Pseudocode for the form’s FormClosing event procedure

Figure 10-46 CD information saved in the CDs.txt file Figure 10-47 Current contents of the CDs.txt file

Page 47: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Coding the Form’s FormClosing Event Procedure (cont.)

Programming with Microsoft Visual Basic 2012 47

Figure 10-48 Code for the CD Collection application (continues)

Page 48: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 48

Figure 10-48 Code for the CD Collection application (continues)

(continued)

Coding the Form’s FormClosing Event Procedure (cont.)

Page 49: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 49

(continued)

Figure 10-48 Code for the CD Collection application (continues)

Coding the Form’s FormClosing Event Procedure (cont.)

Page 50: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

Programming with Microsoft Visual Basic 2012 50

Figure 10-48 Code for the CD Collection application

(continued)

Coding the Form’s FormClosing Event Procedure (cont.)

Page 51: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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

Page 52: Programming with Microsoft Visual Basic 2012 Chapter 10: Structures and Sequential Access Files

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