Upload
bevis-acevedo
View
20
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Programming with Microsoft Visual Basic 2010 5 th Edition. Chapter Ten Structures and Sequential Access Files. Previewing the CD Collection Application. CD Collection application Keeps track of person’s CD collection Saves each CD’s name, artist’s name, and price - PowerPoint PPT Presentation
Citation preview
Chapter TenStructures and Sequential Access
Files
Programming with Microsoft Visual Basic 2010
5th Edition
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the CD Collection Application
2
CD Collection applicationKeeps track of person’s CD collectionSaves each CD’s name, artist’s name, and
priceUses sequential access file named CDs.txtCan add to or remove information from file
Open the CD.exe file
Programming with Microsoft Visual Basic 2010, 5th 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, 5th Edition
Previewing the CD Collection Application (cont’d.)
4
Figure 10-2 Contents of the CDs.txt file
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Objectives
5
After 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 Edition
Structures
6
Structure statementEnables you to create your own data typesUsed to group related items of different data
types into one unitTypically appears in form’s Declaration
section Structure (or user-defined data type)
Data type created with Structure statementMember variables
Variables, constants, or procedures declared within structure declaration
Programming with Microsoft Visual Basic 2010, 5th Edition
Structures (cont’d.)
7
Figure 10-3 Syntax and an example of the Structure statement
Programming with Microsoft Visual Basic 2010, 5th Edition
Declaring and Using a Structure Variable
8
Structure variables: Declared using structureStructure 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, 5th 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, 5th Edition10
Figure 10-5 Examples of using a member variable
Programming with Microsoft Visual Basic 2010, 5th Edition
Passing a Structure Variable to a Procedure
11
Application for sales manager at Willow PoolsAllows user to enter length, width, and depthCalculates gallons of water to fill pool
Advantages 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 Edition12
Figure 10-7 Code for the Willow Pools application (without a structure)
Programming with Microsoft Visual Basic 2010, 5th Edition13
Figure 10-8 Code for the Willow Pools application (with a structure)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Summary
14
Structures: User-defined data typesStructure members can be variables,
constants, or proceduresRefer 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, 5th Edition
Lesson B Objectives
15
After 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 Edition
Sequential Access Files
16
Reading 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 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, 5th Edition
Writing Data to a Sequential Access File
17
Stream of charactersSequence of characters
StreamWriter objectUsed to write stream of characters to
sequential access fileMust declare StreamWriter variable
Game Show Contestants applicationUses StreamWriter variable
Programming with Microsoft Visual Basic 2010, 5th Edition
Writing Data to a Sequential Access File (cont’d.)
18
Figure 10-15 Syntax and an example of declaring a StreamWriter variable
Programming with Microsoft Visual Basic 2010, 5th Edition19
Figure 10-17 Syntax and examples of the CreateText and AppendText methods
Programming with Microsoft Visual Basic 2010, 5th Edition20
Figure 10-18 Syntax and examples of the Write and WriteLine methods
Programming with Microsoft Visual Basic 2010, 5th Edition
Closing an Output Sequential Access File
21
Close methodUsed to close an output sequential access file
Figure 10-19 Syntax and an example of closing an output sequential access file
Programming with Microsoft Visual Basic 2010, 5th Edition
Reading Data from a Sequential Access File
22
StreamReader objectUsed to read data from sequential access fileMust declare StreamReader variable
OpenText methodUsed to open sequential access file for inputCan use this method to automatically create
StreamReader objectExists method
Used to determine if file existsReturns True if file exists, otherwise False
Programming with Microsoft Visual Basic 2010, 5th Edition23
Figure 10-21 Syntax and an example of declaring a StreamReader variable
Programming with Microsoft Visual Basic 2010, 5th Edition24
Figure 10-22 Syntax and an example of the OpenText method
Programming with Microsoft Visual Basic 2010, 5th Edition25
Figure 10-23 Syntax and an example of the Exists method
Programming with Microsoft Visual Basic 2010, 5th Edition26
Figure 10-24 Additional code entered in the procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Reading Data from a Sequential Access File (cont’d.)
27
Line: Sequence (stream) of characters followed by newline character
ReadLine methodUsed to read contents of file, one line at a timeReturns String value containing data in current
lineReturns only data, not including newline
characterPeek method
Determines whether file contains another character to read
Programming with Microsoft Visual Basic 2010, 5th Edition28
Figure 10-25 Syntax and an example of the ReadLine method
Programming with Microsoft Visual Basic 2010, 5th Edition29
Figure 10-26 Syntax and an example of the Peek method
Programming with Microsoft Visual Basic 2010, 5th Edition
Closing an Input Sequential Access File
30
Close methodUsed to close input sequential access files
Figure 10-27 Syntax and an example of closing an input sequential access file