Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic 2010 5 th Edition.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Programming with Microsoft Visual Basic 2005, Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Arrays.
Chapter 8: Manipulating Strings
Chapter 7: Sub and Function Procedures
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Using Arrays and File Handling
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 10: Structures and Sequential Access Files
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter 6: The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Chapter 8: Manipulating Strings
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
1.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Pascal Programming Today Chapter 11 1 Chapter 11.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Chapter 14: Sequential Access Files
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Microsoft Visual Basic 2008: Reloaded Third Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
The Selection Structure
CIS16 Application Development and Programming using Visual Basic.net
Tutorial 9 Sequential Access Files and Printing
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition

Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the CD Collection Application 2 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 2010, 5 th Edition Previewing the CD Collection Application (cont’d.) 3 Figure 10-1 CD information added to the list box

Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the CD Collection Application (cont’d.) 4 Figure 10-2 Contents of the CDs.txt file

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives 5 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 2010, 5 th Edition Structures 6 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 2010, 5 th Edition Structures (cont’d.) 7 Figure 10-3 Syntax and an example of the Structure statement

Programming with Microsoft Visual Basic 2010, 5 th Edition Declaring and Using a Structure Variable 8 Structure variables: Declared using structure Structure is data type for variable Example: Dim hourly As Employee hourly is variable declared with Employee structure type Accessing member variable in code Use structureVariableName. memberVariableName Example: hourly.dblPay = 26 Member variables are used like scalar variables

Programming with Microsoft Visual Basic 2010, 5 th Edition Declaring and Using a Structure Variable (cont’d.) 9 Figure 10-4 Syntax and examples of declaring a structure variable

Programming with Microsoft Visual Basic 2010, 5 th Edition 10 Figure 10-5 Examples of using a member variable

Programming with Microsoft Visual Basic 2010, 5 th Edition Passing a Structure Variable to a Procedure 11 Application for sales manager at Willow Pools Allows user to enter length, width, and depth Calculates gallons of water to fill 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 2010, 5 th Edition 12 Figure 10-7 Code for the Willow Pools application (without a structure)

Programming with Microsoft Visual Basic 2010, 5 th Edition 13 Figure 10-8 Code for the Willow Pools application (with a structure)

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating an Array of Structure Variables 14 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 2010, 5 th Edition 15 Figure 10-9 Code for the Treasures Gift Shop application (without a structure)

Programming with Microsoft Visual Basic 2010, 5 th Edition 16 Figure Names of some of the member variables in the priceList array

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary 17 Structures: User-defined data types Structure members can be variables, constants, or procedures Refer to member within structure variable using structureVariableName.memberVariableName To create an array of structure variables: Declare array using structure as data type Refer to member within structure variable stored in an array using: arrayName(subscript).memberVariableName

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Objectives 18 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 2010, 5 th Edition Sequential Access Files 19 Reading a file: Getting data from a file Writing to a file: Sending data to a file Output files: Store application output Input files: Application uses data in these 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 2010, 5 th Edition Writing Data to a Sequential Access File 20 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 application Uses StreamWriter variable

Programming with Microsoft Visual Basic 2010, 5 th Edition Writing Data to a Sequential Access File (cont’d.) 21 Figure Syntax and an example of declaring a StreamWriter variable

Programming with Microsoft Visual Basic 2010, 5 th Edition 22 Figure Syntax and examples of the CreateText and AppendText methods

Programming with Microsoft Visual Basic 2010, 5 th Edition 23 Figure Syntax and examples of the Write and WriteLine methods

Programming with Microsoft Visual Basic 2010, 5 th Edition Closing an Output Sequential Access File 24 Close method Used to close an output sequential access file Figure Syntax and an example of closing an output sequential access file

Programming with Microsoft Visual Basic 2010, 5 th Edition Reading Data from a Sequential Access File 25 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 2010, 5 th Edition 26 Figure Syntax and an example of declaring a StreamReader variable

Programming with Microsoft Visual Basic 2010, 5 th Edition 27 Figure Syntax and an example of the OpenText method

Programming with Microsoft Visual Basic 2010, 5 th Edition 28 Figure Syntax and an example of the Exists method

Programming with Microsoft Visual Basic 2010, 5 th Edition 29 Figure Additional code entered in the procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Reading Data from a Sequential Access File (cont’d.) 30 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 2010, 5 th Edition 31 Figure Syntax and an example of the ReadLine method

Programming with Microsoft Visual Basic 2010, 5 th Edition 32 Figure Syntax and an example of the Peek method

Programming with Microsoft Visual Basic 2010, 5 th Edition Closing an Input Sequential Access File 33 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 2010, 5 th Edition 34 Figure Click event procedures for the btnWrite and btnRead controls (continues)

Programming with Microsoft Visual Basic 2010, 5 th Edition 35 Figure Click event procedures for the btnWrite and btnRead controls (cont’d.)

Programming with Microsoft Visual Basic 2010, 5 th Edition 36 Figure Five contestant names listed in the Contestants box

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary 37 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 2010, 5 th Edition Lesson C Objectives 38 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 2010, 5 th Edition Coding the CD Collection Application 39 Figure Interface for the CD Collection application

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the CD Collection Application (cont’d.) 40 Figure TOE chart for the CD Collection application

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the CD Collection Application (cont’d.) 41 Figure CDs.txt window

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Form’s Load Event Procedure 42 Figure Pseudocode for the form’s Load event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Form’s Load Event Procedure (cont’d.) 43 Figure Additional comment and code entered in the Load event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Form’s Load Event Procedure (cont’d.) 44 Figure Contents of the CDs.txt file shown in the list box

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnAdd Control’s Click Event Procedure 45 Figure Pseudocode for the btnAdd control’s Click event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Aligning Columns of Information 46 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

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnRemove Control’s Click Event Procedure 47 Main task Allow user to remove selected line from list box control Remove method Removes the item whose value is specified in its item argument RemoveAt method Removes item whose index is specified in its index argument

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnRemove Control’s Click Event Procedure (cont’d.) 48 Figure Pseudocode for the btnRemove control’s Click event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnRemove Control’s Click Event Procedure (cont’d.) 49 Figure Syntax and examples of the Items collection’s Remove and RemoveAt methods

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Form’s FormClosing Event Procedure 50 Figure Pseudocode for the form’s FormClosing event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary 51 To align columns of information: Use PadLeft and PadRight methods To align column of numbers by the decimal point: Format numbers appropriately Use PadLeft to right-align the numbers To include specific number of spaces in string: Use Strings.Space method To remove item from a list box: Use Remove or RemoveAt method