Microsoft Visual Basic 2010: Reloaded Fourth Edition - PowerPoint PPT Presentation

Loading...

PPT – Microsoft Visual Basic 2010: Reloaded Fourth Edition PowerPoint presentation | free to download - id: 7b8ae9-NGM1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Description:

Created Date: 9/27/2002 11:29:22 PM Document presentation format: On-screen Show (4:3) Other titles: Times New Roman Arial Courier New Default Design 1_Default Design ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 55
Provided by: winonaEdu
Category:

less

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

Title: Microsoft Visual Basic 2010: Reloaded Fourth Edition


1
Microsoft Visual Basic 2010 Reloaded Fourth
Edition
  • Chapter Eleven
  • Structures and Sequential Files

2
Objectives
  • After studying this chapter, you should be able
    to
  • Create a structure
  • Declare and use a structure variable
  • Pass a structure variable to a procedure
  • Create an array of structure variables
  • Write data to a sequential access file
  • Close a sequential access file

3
Objectives (cont'd.)
  • Read data from a sequential access file
  • Use the Exists and Peek methods
  • Align columns of information
  • Code the FormClosing event procedure
  • Write and read records
  • Use the Split function

4
Structures
  • Structure statement used to create your own data
    type
  • User-defined data types (or structures) data
    types created by using the Structure statement
  • Member variables variables defined within a
    structure
  • Structure can include member variables of
  • Any standard data types
  • Other structure types
  • Used to group related items into one unit
  • Usually declared in the forms Declarations
    section

5
Structures (cont'd.)
Figure 11-1 How to define a structure
6
Declaring a Structure Variable
  • Structure statement only creates the data type
  • Structure variables variables declared using a
    structure

Figure 11-2 How to declare a structure variable
7
Declaring a Structure Variable (cont'd.)
  • 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

8
Declaring a Structure Variable (cont'd.)
Figure 11-3 How to use a member variable
9
Passing a Structure Variable to a Procedure
  • Sample application without using a structure

Figure 11-4 Sample run of the Willow Pools
application
10
Figure 11-5 Code for the Willow Pools
application (without a structure)
11
Figure 11-6 Code for the Willow Pools
application (with a structure)
12
Passing a Structure Variable to a Procedure
(cont'd.)
  • When you pass a structure variable to a
    procedure, all of its member variables are
    automatically passed
  • Sample application using a structure
  • Uses less code to pass a structure variable to a
    procedure
  • Stores all of the data in a single unit

13
Creating an Array of Structure Variables
  • Use a one-dimensional array of structure
    variables
  • Each element in the array is a structure variable

Figure 11-7 Sample run of the Treasures Gift
Shop application
14
Figure 11-8 Partial code for the Treasures Gift
Shop application
15
Creating an Array of Structure Variables
(contd.)
Figure 11-8 Partial code for the Treasures Gift
Shop application (contd.)
16
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
  • Sequential access files (text files) composed of
    text that are both read and written sequentially,
    one line at a time

17
Writing Data to a Sequential Access File
  • Stream of characters a sequence of characters
  • StreamWriter object used to write a stream of
    characters to a sequential access file
  • Use IO.File methods to manipulate the file

18
Writing Data to a Sequential Access File
(cont'd.)
Figure 11-9 How to declare a StreamWriter
variable
19
Writing Data to a Sequential Access File
(cont'd.)
  • CreateText method opens a sequential access file
    for output
  • Creates a new, empty file to which data can be
    written
  • If the named file already exists, its contents
    are erased before writing to it
  • AppendText method opens a sequential access file
    for append
  • New data is written after any existing data in
    the file
  • If the named file does not exist, it is created
    for you

20
Figure 11-10 How to create a StreamWriter object
21
Writing Data to a Sequential Access File
(cont'd.)
  • After opening a file for output or append, you
    can begin writing data to it
  • Write method writes a line of data to the file
  • WriteLine method writes a line of data to the
    file and writes a newline character after the
    data

22
Writing Data to a Sequential Access File
(cont'd.)
Figure 11-11 How to write to a sequential access
file
23
Closing an Output Sequential Access File
  • Close method used to close an output sequential
    access file
  • Ensures that the data is saved
  • Makes the file available for use elsewhere in the
    application
  • Uses a StreamWriter variable to refer to the file
    in code

24
Closing an Output Sequential Access File
(contd.)
Figure 11-12 How to close an output sequential
access file
25
Reading Data from a Sequential Access File
  • StreamReader object used to read data from a
    sequential access file
  • Must first declare a StreamReader variable
  • OpenText method used to open a sequential access
    file for input
  • Automatically creates a StreamReader object
  • Arguments
  • fileName the filename and optionally the path to
    the file

26
Reading Data from a Sequential Access File
(cont'd.)
Figure 11-13 How to declare a StreamReader
variable
27
Reading Data from a Sequential Access File
(cont'd.)
Figure 11-14 How to create a StreamReader object
28
Reading Data from a Sequential Access File
(cont'd.)
  • If an input file cannot be located at run time,
    an error occurs
  • You can avoid the error by determining if the
    file exists before trying to open it
  • Exists method returns a Boolean value of True if
    a file exists otherwise returns a Boolean value
    of False

29
Reading Data from a Sequential Access File
(cont'd.)
Figure 11-15 How to determine whether a
sequential access file exists
30
Reading Data from a Sequential Access File
(cont'd.)
  • Line a sequence (stream) of characters followed
    by the newline character
  • ReadLine method used to read the contents of a
    file, one line at a time
  • Does not include the newline character at the end
    of the line
  • Uses the file associated with the StreamReader
    variable

31
Reading Data from a Sequential Access File
(cont'd.)
Figure 11-16 How to read data from a sequential
access file
32
Reading Data from a Sequential Access File
(cont'd.)
  • Peek method peeks into the file to determine
    whether the file contains another character to
    read
  • If there is another character, Peek returns the
    character
  • If there are no more characters, Peek returns a
    value of -1

33
Reading Data from a Sequential Access File
(cont'd.)
Figure 11-17 How to use the Peek method
34
Closing an Input Sequential Access File
  • Use the Close method to close an input sequential
    access file when you are finished using it
  • Makes the file available for use elsewhere in the
    application

Figure 11-18 How to close an input sequential
access file
35
The Game Show Contestants Application
  • Sample application used to record the names of
    game show contestants in a sequential access file

Figure 11-19 Sample run of the Game Show
Contestants application
36
Figure 11-20 Partial code for the Game Show
Contestants application
37
The Game Show Contestants Application (contd.)
Figure 11-20 Partial code for the Game Show
Contestants application (contd.)
38
Aligning Columns of Information
  • PadLeft, PadRight methods
  • Pads a string with a specified number of
    characters based on current length of the string
    being padded
  • Used to align columns of information written to a
    sequential access file
  • The default pad character is the space character
  • Align columns of numeric information by the
    decimal point

39
Figure 11-22 How to align columns of information
40
Aligning Columns of Information (cont'd.)
Figure 11-22 How to align columns of information
(cont'd.)
41
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
  • Form may be closed by Me.Close() statement or by
    user clicking the Close button on title bar

42
The FormClosing Event (cont'd.)
Figure 11-23 How to use the FormClosing event
procedure
43
The FormClosing Event (cont'd.)
Figure 11-23 How to use the FormClosing event
procedure (cont'd.)
44
Writing and Reading Records
  • Field a single item of information about a
    person, place, or thing
  • Record a group of related fields that contain
    all of the necessary data about a specific
    person, place, or thing
  • If a record contains more than one field, the
    fields are separated by a delimiter character

45
Figure 11-24 How to write records to a
sequential access file
46
Writing and Reading Records (contd.)
  • Split function used with ReadLine to divide a
    line of text from a file into fields
  • Assigns each field to an element in an array
  • Must specify the delimiter that was used in the
    file

47
Writing and Reading Records (contd.)
Figure 11-25 How to read records from a
sequential access file
48
Writing and Reading Records (contd.)
Figure 11-25 How to read records from a
sequential access file (contd.)
49
Programming Tutorial 1
  • Modifying the Concentration Game Application
  • Use Concentration Game application from Ch. 8

Figure 11-26 Contents of three of the four
sequential access files
50
Programming Tutorial 2
  • Creating the CD Collection Application

Figure 11-30 MainForm for the CD Collection
application
51
Programming Example
  • Glovers Industries Application
  • Display items and prices that are stored in a
    sequential access file

Figure 11-40 MainForm in the Glovers Industries
application
52
Summary
  • Structure statement defines a user-defined data
    type or structure
  • Structure variable contains one or more member
    variables
  • Structure variable can be passed to procedures
  • Can create a one-dimensional array of structure
    variables
  • Use the dot member access operator to access the
    structure member variables

53
Summary (cont'd.)
  • Application can read from and write to a file
  • Sequential access files are always accessed in
    consecutive (sequential) order from beginning to
    end
  • StreamWriter variable used to write data to a
    sequential access file
  • StreamReader variable used to read data from a
    sequential access file
  • Close method closes a file

54
Summary (cont'd.)
  • Use PadLeft and PadRight methods to align data in
    a sequential access file
  • FormClosing event occurs when the form is about
    to be closed
  • Fields in a record are separated by a delimiter
    character
  • Use the Split function to read delimited records
    from a file
About PowerShow.com