programming with microsoft visual basic 2010 5 th edition

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

Upload: bevis-acevedo

Post on 01-Jan-2016

23 views

Category:

Documents


0 download

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

TRANSCRIPT

Page 1: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Chapter TenStructures and Sequential Access

Files

Programming with Microsoft Visual Basic 2010

5th Edition

Page 2: Programming with  Microsoft Visual Basic 2010  5 th  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

Page 3: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 4: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 5: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 6: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 7: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition

Structures (cont’d.)

7

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

Page 8: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 9: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 10: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition10

Figure 10-5 Examples of using a member variable

Page 11: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 12: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition12

Figure 10-7 Code for the Willow Pools application (without a structure)

Page 13: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition13

Figure 10-8 Code for the Willow Pools application (with a structure)

Page 14: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 15: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 16: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 17: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 18: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 19: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition19

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

Page 20: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition20

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

Page 21: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 22: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 23: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition23

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

Page 24: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition24

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

Page 25: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition25

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

Page 26: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition26

Figure 10-24 Additional code entered in the procedure

Page 27: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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

Page 28: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition28

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

Page 29: Programming with  Microsoft Visual Basic 2010  5 th  Edition

Programming with Microsoft Visual Basic 2010, 5th Edition29

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

Page 30: Programming with  Microsoft Visual Basic 2010  5 th  Edition

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