Programming with Microsoft Visual Basic 2008 Fourth Edition - PowerPoint PPT Presentation

Loading...

PPT – Programming with Microsoft Visual Basic 2008 Fourth Edition PowerPoint presentation | free to download - id: 41f6a0-N2ZlZ



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Programming with Microsoft Visual Basic 2008 Fourth Edition

Description:

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 48
Provided by: facultyTa4
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Programming with Microsoft Visual Basic 2008 Fourth Edition


1
Programming with Microsoft Visual Basic 2008
Fourth Edition
  • Chapter Ten
  • Structures and Sequential Access Files

2
Previewing the CD Collection Application
  • CD Collection Application Keeps track of
    persons CD collection
  • Saves each CDs name, artists name, and price
  • Uses sequential access file named CDs.txt
  • Can add to or remove information from file
  • Open the CD.exe file

3
Previewing the CD Collection Application
(continued)
Figure 10-1 CD information added to the list box
4
Previewing the CD Collection Application
(continued)
Figure 10-2 Contents of the CDs.txt file
5
Lesson A Objectives
  • After studying Lesson A, you should be able to
  • Create a structure
  • Declare a structure variable
  • Pass a structure variable to a procedure
  • Create and manipulate a one-dimensional array of
    structures

6
Creating a Structure
  • Structure statement
  • Enables you to create your own data types
  • Used to group related items of different data
    types into one unit
  • Typically appears in forms Declaration section
  • Structure (or user-defined data type) Data type
    created with Structure statement
  • Member variables
  • Variables, constants, or procedures declared
    within structure declaration

7
Creating a Structure (continued)
Figure 10-3 Syntax and an example of the
Structure statement
8
Declaring and Using a Structure Variable
  • Structure variables Declared using structure
  • Structure is data type for variable
  • Example Dim manager As Employee
  • manager is variable declared with Employee
    structure type
  • Accessing member variable in code
  • Use structureVariableName. memberName
  • Example manager.dblSalary 59000D
  • Member variables are used like scalar variables

9
Declaring and Using a Structure Variable
(continued)
Figure 10-4 Syntax and examples of declaring a
structure variable
10
Declaring and Using a Structure Variable
(continued)
Figure 10-5 Examples of using a member variable
11
Passing a Structure Variable to a Procedure
  • Application for sales manager at Willows Pool
  • Allows user to enter length, width, and depth
  • Calculates volume of pool
  • Advantages of using structure to group
    dimensions
  • Three inputs are stored in one structure variable
  • You pass single structure variable to procedure
    instead of three scalar variables
  • Your code is structured in more readable form

12
Passing a Structure Variable to a Procedure
(continued)
Figure 10-7 Code for the Willow Pools
application (without a structure)
13
Passing a Structure Variable to a Procedure
(continued)
Figure 10-8 Code for the Willow Pools
application (with a structure)
14
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)
  • One-dimensional array of structure variables
  • Structure variable will contain
  • String variable for ID
  • Integer variable for price

15
Creating an Array of Structure Variables
(continued)
Figure 10-9 Code for the Treasures Gift Shop
application (without a structure)
16
Creating an Array of Structure Variables
(continued)
Figure 10-10 Names of some of the member
variables in the priceList array
17
Lesson A Summary
  • Structures are user-defined data types
  • Structure members can be variables, constants, or
    procedures
  • Refer to member within structure variable using
    structureVariableName.memberName
  • Element in array of structure variables is
    structure variable
  • Refer to member within structure variable stored
    in an array using arrayName(subscript).memberName

18
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

19
Sequential Access Files
  • Reading a file Getting data from a file
  • Writing to a file Sending data to a file
  • Output files Files to which information is
    written
  • Input files Files that are read by a computer
  • Sequential access files Files composed of lines
    of text that are both read and written
    sequentially

20
Writing Data to a Sequential Access File
  • Stream of characters Sequence of characters
  • StreamWriter object Used to write stream of
    characters to sequential access file
  • Must declare StreamWriter variable
  • Game Show Contestants sample application uses
    StreamWriter variable

21
Writing Data to a Sequential Access File
(continued)
Figure 10-15 Syntax and an example of declaring
a StreamWriter variable
22
Writing Data to a Sequential Access File
(continued)
Figure 10-17 Syntax and examples of creating a
StreamWriter object by opening a file
23
Writing Data to a Sequential Access File
(continued)
Figure 10-18 Syntax and examples of writing data
to a sequential access file
24
Writing Data to a Sequential Access File
(continued)
Figure 10-19 Names contained in the
contestants.txt file
25
Reading Data From a Sequential Access File
  • StreamReader object Used to read data from
    sequential access file
  • Must declare StreamReader variable
  • OpenText method Used to open sequential access
    file for input
  • Can use this method to automatically create
    StreamReader object
  • Exists method Used to determine if file exists
  • Returns True if file exists, otherwise False

26
Reading Data From a Sequential Access File
(continued)
Figure 10-21 Syntax and an example of creating a
StreamReader object by opening a file
27
Reading Data From a Sequential Access File
(continued)
Figure 10-22 Syntax and an example of the Exists
method
28
Reading Data From a Sequential Access File
(continued)
Figure 10-23 Code entered in the selection
structures false path
29
Reading Data From a Sequential Access File
(continued)
  • Line Sequence (stream) of characters followed by
    newline character
  • ReadLine method Used to read contents of file,
    one line at a time
  • Returns String value containing data in current
    line
  • Returns only data, not including newline
    character
  • Peek method Determines whether file contains
    another character to read

30
Reading Data From a Sequential Access File
(continued)
Figure 10-24 Syntax and examples of reading data
from a sequential access file
31
Reading Data From a Sequential Access File
(continued)
Figure 10-25 Code entered in the btnWrite and
btnRead controls Click event procedures
32
Reading Data From a Sequential Access File
(continued)
Figure 10-25 Code entered in the btnWrite and
btnRead controls Click event procedures
(continued)
33
Reading Data From a Sequential Access File
(continued)
Figure 10-26 Contestant names appear in the
Contestants box
34
Lesson B Summary
  • Sequential access file Stores data items in
    consecutive order (sequentially)
  • Use StreamWriter variable to write data to
    sequential access file
  • Use StreamReader variable to read data from
    sequential access file
  • Use Exists method to determine if file exists
  • Use Peek method to determine whether end of
    sequential access file has been reached

35
Lesson C Objectives
  • After studying Lesson C, you should be able to
  • Fill a list box with values stored in a
    sequential access file
  • 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

36
Coding the CD Collection Application
Figure 10-28 User interface for the CD
Collection application
37
Coding the CD Collection Application (continued)
Figure 10-29 TOE chart for the CD Collection
application
38
Coding the CD Collection Application (continued)
Figure 10-30 CDs.txt window showing the contents
of the sequential access file
39
Coding the Forms Load Event Procedure
Figure 10-31 Pseudocode for the forms Load
event procedure
40
Coding the Forms Load Event Procedure (continued)
Figure 10-32 Additional comment and code entered
in the forms Load event procedure
41
Coding the Forms Load Event Procedure (continued)
Figure 10-33 The contents of the CDs.txt file
appear in the list box
42
Coding the btnAdd Controls Click Event Procedure
Figure 10-34 Pseudocode for the btnAdd controls
Click event procedure
43
Aligning Columns of Information
  • PadLeft and PadRight methods Used to pad strings
    with characters
  • These methods can be used to align text in list
    box or text written to sequential access file
  • Strings.Space method Used to include specific
    number of space characters in string
  • Syntax Strings.Space(number)
  • number Integer representing number of spaces to
    include

44
Coding the btnRemove Controls Click Event
Procedure
  • Main task Allow user to remove selected line
    from list box control
  • RemoveAt method Removes list box item at
    specified index

45
Coding the btnRemove Controls Click Event
Procedure (continued)
Figure 10-37 Pseudocode for the btnRemove
controls Click event procedure
46
Coding the Forms FormClosing Event Procedure
Figure 10-38 Pseudocode for the forms
FormClosing event procedure
47
Lesson C Summary
  • Use Add method of list box to add an item to list
  • Use PadLeft method to right-align string, or to
    align column of numbers by decimal point
  • Use PadRight method to left-align string
  • Use Strings.Space method to include specific
    number of spaces in string
  • Use RemoveAt method to remove item at specific
    index from listbox
About PowerShow.com