microsoft visual basic 2005: reloaded second edition chapter 9 structures and sequential access...

30
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Post on 20-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded

Second Edition

Chapter 9Structures and Sequential Access Files

Page 2: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 2

Structures

• Structure statement: creates your own data type• User-defined data type (or structure): data types

created by using the Structure statement• Member variables: variables defined within a

structure• Structure can include member variables of:

– Any of the standard data types– Other structure types

Page 3: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 3

Structures (continued)

Page 4: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 4

Using a Structure to Declare a Variable

• Structure variables: variables declared using a structure

• Refer to an entire structure in code using its name

• Refer to a member variable using the structure name with the dot operator and the member variable name

• Member variables can be used like any other variables

• Structures are used to group related items into one unit

Page 5: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 5

Using a Structure to Declare a Variable (continued)

Page 6: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 6

Using a Structure to Declare a Variable (continued)

Page 7: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 7

Passing a Structure Variable to a Procedure

Open the Pool program and Flow Chart

Page 8: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 8

Passing a Structure Variable to a Procedure (continued)

• Sample application using a structure:– Less code to pass a structure variable to a

procedure– Stores all of the data in a single unit

Page 9: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 9

Creating an Array of Structure Variables

• Use a one-dimensional array of structure variables instead of two-dimensional or parallel one-dimensional arrays

• Refer to a member variable in an array element by:

arrayname(subscript).memberVariableName

Page 10: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 10

Creating an Array of Structure Variables (continued)

Page 11: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 11

Creating an Array of Structure Variables (continued)

Page 12: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 12

Creating an Array of Structure Variables (continued)

Page 13: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 13

Creating an Array of Structure Variables (continued)

Page 14: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 14

File Types

• Reading a file: getting information from a file

• Writing to a file: sending information to a file

• Output files: files to which information is written

• Input files: files that are read by the computer

• Three types of files in Visual Basic:– Sequential – Random– Binary

Page 15: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 15

File Types (continued)

• Sequential file: data is accessed sequentially, in consecutive order from the beginning to the end

• Random file: data can be accessed in consecutive or random order

• Binary file: data is accessed by its byte location in the file

Page 16: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 16

Sequential Access Files

• Sequential access file: often called a text file because it is composed of lines of text

• Data must be read sequentially from beginning to end

Page 17: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 17

Sequential Access Files (continued)

Page 18: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 18

Writing Information to a Sequential Access File

• WriteAllText method: – Writes information to a sequential access file– File argument: contains the name of the file to be

used– Text argument: contains the data to be written– Append argument: if True, data is added to end of

existing file; otherwise all existing data is erased

Page 19: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 19

Writing Information to a Sequential Access File (continued)

• My feature: exposes a set of commonly used objects to the programmer

• My.Computer object: allows access to objects and methods to manipulate files

• Computer uses a file pointer to trace where the next character will be read from or written to a file

• Strings.Space method: writes a specific number of spaces to a file

Page 20: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 20

Writing Information to a Sequential Access File (continued)

Page 21: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 21

Aligning Columns of Information in a Sequential Access File

• PadLeft, PadRight methods: – Pads a string with a specified number of characters

based on current length of the string being padded– Aligns columns of information written to a sequential

access file

• Aligns columns of numeric information by the decimal point

Page 22: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 22

Aligning Columns of Information in a Sequential Access File (continued)

Page 23: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 23

Aligning Columns of Information in a Sequential Access File (continued)

Page 24: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 24

Reading Information from a Sequential Access File

• ReadAllText method: reads information stored in a sequential access file– File argument: contains the name of the sequential

access file

Page 25: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 25

Reading Information from a Sequential Access File (continued)

Page 26: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 26

Determining Whether a File Exists

• An attempt to read a file that does not exist will cause an error

• FileExists method: determines if the file exists before attempting to read it– File argument: contains the name of the file

Page 27: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 27

Determining Whether a File Exists (continued)

Page 28: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 28

The FormClosing Event

• FormClosing event: – Occurs when a form is about to be closed by the

program code or by the user– Allows you to trap the closing action and take any

necessary actions such as saving data– Can be used to cancel the close action– Set e.Cancel = True to cancel the closing action

Page 29: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 29

The FormClosing Event (continued)

Page 30: Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files

Microsoft Visual Basic 2005: Reloaded, Second Edition 30

The Friends Application