Click here to load reader

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

Embed Size (px)

Citation preview

Programming with Microsoft Visual Basic 2008 Fourth Edition

Chapter TenStructures and Sequential Access FilesProgramming with Microsoft Visual Basic 2010 5th Edition1Previewing the CD Collection Application2CD Collection applicationKeeps track of persons CD collectionSaves each CDs name, artists name, and priceUses sequential access file named CDs.txtCan add to or remove information from fileOpen the CD.exe file

Programming with Microsoft Visual Basic 2010, 5th Edition2Previewing the CD Collection Application (contd.)3Figure 10-1 CD information added to the list box

Programming with Microsoft Visual Basic 2010, 5th Edition3Previewing the CD Collection Application (contd.)4Figure 10-2 Contents of the CDs.txt file

Programming with Microsoft Visual Basic 2010, 5th Edition4Lesson A Objectives 5After studying Lesson A, you should be able to:Create a structureDeclare and use a structure variablePass a structure variable to a procedureCreate an array of structure variables

Programming with Microsoft Visual Basic 2010, 5th Edition5Structures6Structure statementEnables you to create your own data typesUsed to group related items of different data types into one unitTypically appears in forms Declaration section Structure (or user-defined data type)Data type created with Structure statementMember variablesVariables, constants, or procedures declared within structure declarationProgramming with Microsoft Visual Basic 2010, 5th Edition6Structures (contd.)7

Figure 10-3 Syntax and an example of the Structure statementProgramming with Microsoft Visual Basic 2010, 5th EditionDeclaring and Using a Structure Variable8Structure variables: Declared using structureStructure is data type for variableExample: Dim hourly As Employee hourly is variable declared with Employee structure type Accessing member variable in codeUse structureVariableName. memberVariableNameExample: hourly.dblPay = 26 Member variables are used like scalar variablesProgramming with Microsoft Visual Basic 2010, 5th Edition8Declaring and Using a Structure Variable (contd.)9Figure 10-4 Syntax and examples of declaring a structure variable

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

Programming with Microsoft Visual Basic 2010, 5th EditionPassing a Structure Variable to a Procedure11Application for sales manager at Willow PoolsAllows user to enter length, width, and depthCalculates gallons of water to fill poolAdvantages of using structure to group dimensionsThree inputs are stored in one structure variableYou pass single structure variable to procedure instead of three scalar variablesYour code is structured in more readable form Programming with Microsoft Visual Basic 2010, 5th Edition1112

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

Programming with Microsoft Visual Basic 2010, 5th EditionCreating an Array of Structure Variables14Three ways to manage pairs of ID-price dataTwo parallel one-dimensional arraysOne two-dimensional array (tabular format)One-dimensional array of structure variablesStructure variable will contain:String variable for IDInteger variable for priceProgramming with Microsoft Visual Basic 2010, 5th Edition1415Figure 10-9 Code for the Treasures Gift Shop application (without a structure)

Programming with Microsoft Visual Basic 2010, 5th Edition16Figure 10-10 Names of some of the member variables in the priceList array

Programming with Microsoft Visual Basic 2010, 5th EditionLesson A Summary17Structures: User-defined data typesStructure members can be variables, constants, or proceduresRefer to member within structure variable using structureVariableName.memberVariableNameTo create an array of structure variables:Declare array using structure as data typeRefer to member within structure variable stored in an array using: arrayName(subscript).memberVariableNameProgramming with Microsoft Visual Basic 2010, 5th Edition17Lesson B Objectives18After studying Lesson B, you should be able to:Open and close a sequential access fileWrite data to a sequential access fileRead data from a sequential access fileDetermine whether a sequential access file existsTest for the end of a sequential access file

Programming with Microsoft Visual Basic 2010, 5th Edition18Sequential Access Files19Reading a file: Getting data from a fileWriting to a file: Sending data to a fileOutput files: Store application outputInput files: Application uses data in these filesSequential access filesComposed of lines of text that are both read and written in consecutive orderAlso called text filesProgramming with Microsoft Visual Basic 2010, 5th Edition19Writing Data to a Sequential Access File20Stream of charactersSequence of charactersStreamWriter objectUsed to write stream of characters to sequential access fileMust declare StreamWriter variableGame Show Contestants applicationUses StreamWriter variableProgramming with Microsoft Visual Basic 2010, 5th Edition20Writing Data to a Sequential Access File (contd.)21Figure 10-15 Syntax and an example of declaring a StreamWriter variable

Programming with Microsoft Visual Basic 2010, 5th Edition22Figure 10-17 Syntax and examples of the CreateText and AppendText methods

Programming with Microsoft Visual Basic 2010, 5th Edition23Figure 10-18 Syntax and examples of the Write and WriteLine methods

Programming with Microsoft Visual Basic 2010, 5th EditionClosing an Output Sequential Access File24Close methodUsed to close an output sequential access file

Figure 10-19 Syntax and an example of closing an output sequential access fileProgramming with Microsoft Visual Basic 2010, 5th EditionReading Data from a Sequential Access File25StreamReader objectUsed to read data from sequential access fileMust declare StreamReader variableOpenText methodUsed to open sequential access file for inputCan use this method to automatically create StreamReader objectExists methodUsed to determine if file existsReturns True if file exists, otherwise FalseProgramming with Microsoft Visual Basic 2010, 5th Edition2526Figure 10-21 Syntax and an example of declaring a StreamReader variable

Programming with Microsoft Visual Basic 2010, 5th Edition27Figure 10-22 Syntax and an example of the OpenText method

Programming with Microsoft Visual Basic 2010, 5th Edition28Figure 10-23 Syntax and an example of the Exists method

Programming with Microsoft Visual Basic 2010, 5th Edition29Figure 10-24 Additional code entered in the procedure

Programming with Microsoft Visual Basic 2010, 5th EditionReading Data from a Sequential Access File (contd.)30Line: Sequence (stream) of characters followed by newline characterReadLine methodUsed to read contents of file, one line at a timeReturns String value containing data in current lineReturns only data, not including newline characterPeek methodDetermines whether file contains another character to readProgramming with Microsoft Visual Basic 2010, 5th Edition3031Figure 10-25 Syntax and an example of the ReadLine method

Programming with Microsoft Visual Basic 2010, 5th Edition32Figure 10-26 Syntax and an example of the Peek method

Programming with Microsoft Visual Basic 2010, 5th EditionClosing an Input Sequential Access File33Close methodUsed to close input sequential access files

Figure 10-27 Syntax and an example of closing an input sequential access fileProgramming with Microsoft Visual Basic 2010, 5th Edition34Figure 10-28 Click event procedures for the btnWrite and btnRead controls (continues)

Programming with Microsoft Visual Basic 2010, 5th Edition35Figure 10-28 Click event procedures for the btnWrite and btnRead controls (contd.)

Programming with Microsoft Visual Basic 2010, 5th Edition36Figure 10-29 Five contestant names listed in the Contestants box

Programming with Microsoft Visual Basic 2010, 5th EditionLesson B Summary37Sequential access fileStores data items in consecutive order (sequentially)Use StreamWriter variable to write data to sequential access fileUse StreamReader variable to read data from sequential access fileUse Exists method to determine if file existsUse Peek method to determine whether end of sequential access file has been reachedProgramming with Microsoft Visual Basic 2010, 5th Edition37Lesson C Objectives 38After studying Lesson C, you should be able to:Add an item to a list box while an application is runningAlign columns of informationRemove an item from a list box while an application is runningSave list box items in a sequential access fileWrite records to a sequential access fileProgramming with Microsoft Visual Basic 2010, 5th Edition38Coding the CD Collection Application39Figure 10-32 Interface for the CD Collection application

Programming with Microsoft Visual Basic 2010, 5th Edition39Coding the CD Collection Application (contd.)40Figure 10-33 TOE chart for the CD Collection application

Programming with Microsoft Visual Basic 2010, 5th Edition40Coding the CD Collection Application (contd.)41Figure 10-34 CDs.txt window

Programming with Microsoft Visual Basic 2010, 5th Edition41Coding the Forms Load Event Procedure42Figure 10-35 Pseudocode for the forms Load event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition42Coding the Forms Load Event Procedure (contd.)43Figure 10-36 Additional comment and code entered in the Load event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition43Coding the Forms Load Event Procedure (contd.)44Figure 10-37 Contents of the CDs.txt file shown in the list box

Programming with Microsoft Visual Basic 2010, 5th Edition44Coding the btnAdd Controls Click Event Procedure45Figure 10-38 Pseudocode for the btnAdd controls Click event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition45Aligning Columns of Information46PadLeft and PadRight methodsUsed to pad strings with characters These methods can be used to align text in list box or text written to sequential access fileStrings.Space methodUsed to include specific number of space characters in stringSyntax: Strings.Space(number)number: Integer representing number of spaces to include

Programming with Microsoft Visual Basic 2010, 5th Edition46Coding the btnRemove Controls Click Event Procedure47Main taskAllow user to remove selected line from list box controlRemove methodRemoves the item whose value is specified in its item argumentRemoveAt methodRemoves item whose index is specified in its index argumentProgramming with Microsoft Visual Basic 2010, 5th Edition47Coding the btnRemove Controls Click Event Procedure (contd.)48Figure 10-41 Pseudocode for the btnRemove controls Click event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition48Coding the btnRemove Controls Click Event Procedure (contd.)49Figure 10-42 Syntax and examples of the Items collections Remove and RemoveAt methods

Programming with Microsoft Visual Basic 2010, 5th Edition49Coding the Forms FormClosing Event Procedure 50Figure 10-43 Pseudocode for the forms FormClosing event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition50Lesson C Summary51To align columns of information:Use PadLeft and PadRight methodsTo align column of numbers by the decimal point:Format numbers appropriatelyUse PadLeft to right-align the numbersTo include specific number of spaces in string:Use Strings.Space methodTo remove item from a list box:Use Remove or RemoveAt method

Programming with Microsoft Visual Basic 2010, 5th Edition51