Chapter 10: Structures and Sequential Access Files

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

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
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
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
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.
Chapter 4: The Selection Structure
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
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
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 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
1.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Pascal Programming Today Chapter 11 1 Chapter 11.
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.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Microsoft Visual Basic 2008: Reloaded Third Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Variables and Constants
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
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
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Chapter 10: Structures and Sequential Access Files

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

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

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

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

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

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

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

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

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

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

Creating an Array of Structure Variables (cont.) Figure 10-9 Problem specification for the Treasures Gift Shoppe Figure 10-10 Interface for the Treasures Gift Shoppe application Programming with Microsoft Visual Basic 2012

Creating an Array of Structure Variables (cont.) Figure 10-11 Code for the Treasures Gift Shoppe application (without a structure) Programming with Microsoft Visual Basic 2012

Creating an Array of Structure Variables (cont.) Figure 10-12 Syntax and examples of referring to member variables in an array Programming with Microsoft Visual Basic 2012

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 Programming with Microsoft Visual Basic 2012

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

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

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

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

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

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 Programming with Microsoft Visual Basic 2012

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 10-19 Syntax and examples of the CreateText and AppendText methods Programming with Microsoft Visual Basic 2012

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 10-20 Syntax and examples of the Write and WriteLine methods Programming with Microsoft Visual Basic 2012

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 10-21 Syntax and an example of closing an output sequential access file Programming with Microsoft Visual Basic 2012

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

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 Programming with Microsoft Visual Basic 2012

Reading Data from a Sequential Access File (cont.) Figure 10-26 Additional code entered in the procedure Programming with Microsoft Visual Basic 2012

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

Reading Data from a Sequential Access File (cont.) Figure 10-27 Syntax and an example of the ReadLine method Figure 10-28 Syntax and an example of the Peek method Programming with Microsoft Visual Basic 2012

Closing an Input Sequential Access File Close method Used to close input sequential access files Figure 10-29 Syntax and an example of closing an input sequential access file Programming with Microsoft Visual Basic 2012

Closing an Input Sequential Access File (cont.) Figure 10-30 Click event procedures for the btnWrite and btnRead controls (continues) Programming with Microsoft Visual Basic 2012

Closing an Input Sequential Access File (cont.) (continued) Figure 10-30 Click event procedures for the btnWrite and btnRead controls Programming with Microsoft Visual Basic 2012

Closing an Input Sequential Access File (cont.) Figure 10-31 Five contestant names listed in the Contestants box Figure 10-32 Nine contestant names listed in the list box Programming with Microsoft Visual Basic 2012

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

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

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

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

Coding the CD Collection Application 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 Programming with Microsoft Visual Basic 2012

Coding the Form’s Load Event Procedure Figure 10-37 Pseudocode for the form’s Load event procedure Figure 10-38 Additional comment and code entered in the Load event procedure Programming with Microsoft Visual Basic 2012

Coding the Form’s Load Event Procedure (cont.) Figure 10-39 Contents of the CDs.txt file shown in the list box Programming with Microsoft Visual Basic 2012

Coding the btnAdd_Click Procedure Figure 10-40 Pseudocode for the btnAdd_Click procedure Programming with Microsoft Visual Basic 2012

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

Aligning Columns of Information (cont.) Figure 10-41 Examples of aligning columns of information Programming with Microsoft Visual Basic 2012

Aligning Columns of Information (cont.) Figure 10-42 CD information added to the list box Programming with Microsoft Visual Basic 2012

Coding the btnRemove_Click Procedure Figure 10-43 Pseudocode for the btnRemove_Click procedure Figure 10-44 Syntax and examples of the Items collection’s Remove and RemoveAt methods Programming with Microsoft Visual Basic 2012

Coding the Form’s FormClosing Event Procedure 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 Programming with Microsoft Visual Basic 2012

Coding the Form’s FormClosing Event Procedure (cont.) Figure 10-48 Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012

Coding the Form’s FormClosing Event Procedure (cont.) (continued) Figure 10-48 Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012

Coding the Form’s FormClosing Event Procedure (cont.) (continued) Figure 10-48 Code for the CD Collection application (continues) Programming with Microsoft Visual Basic 2012

Coding the Form’s FormClosing Event Procedure (cont.) (continued) Figure 10-48 Code for the CD Collection application Programming with Microsoft Visual Basic 2012

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

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