460 likes | 699 Views
Previewing the CD Collection Application. CD Collection Application: Keeps 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 fileOpen the CD.exe file . Programming with Microsoft Visual Basic 2008, Fourth Edition.
E N D
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 person’s CD collection
Saves each CD’s name, artist’s name, and price
Uses sequential access file named CDs.txt
Can add to or remove information from file
Open the CD.exe file
Programming with Microsoft Visual Basic 2008, Fourth Edition 2
3. Previewing the CD Collection Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 3
4. Previewing the CD Collection Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 4
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
Programming with Microsoft Visual Basic 2008, Fourth Edition 5
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 form’s Declaration section
Structure (or user-defined data type): Data type created with Structure statement
Member variables:
Variables, constants, or procedures declared within structure declaration Programming with Microsoft Visual Basic 2008, Fourth Edition 6
7. Creating a Structure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 7
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 8
9. Declaring and Using a Structure Variable (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 9
10. Declaring and Using a Structure Variable (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 10
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 11
12. Programming with Microsoft Visual Basic 2008, Fourth Edition 12 Passing a Structure Variable to a Procedure (continued)
13. Programming with Microsoft Visual Basic 2008, Fourth Edition 13 Passing a Structure Variable to a Procedure (continued)
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 14
15. Programming with Microsoft Visual Basic 2008, Fourth Edition 15 Creating an Array of Structure Variables (continued)
16. Creating an Array of Structure Variables (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 16
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 17
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
Programming with Microsoft Visual Basic 2008, Fourth Edition 18
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 19
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 20
21. Writing Data to a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 21
22. Writing Data to a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 22
23. Writing Data to a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 23
24. Writing Data to a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 24
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 25
26. Reading Data From a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 26
27. Reading Data From a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 27
28. Reading Data From a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 28
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 29
30. Reading Data From a Sequential Access File (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 30
31. Programming with Microsoft Visual Basic 2008, Fourth Edition 31 Reading Data From a Sequential Access File (continued)
32. Programming with Microsoft Visual Basic 2008, Fourth Edition 32 Reading Data From a Sequential Access File (continued)
33. Programming with Microsoft Visual Basic 2008, Fourth Edition 33
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 34
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 Programming with Microsoft Visual Basic 2008, Fourth Edition 35
36. Coding the CD Collection Application Programming with Microsoft Visual Basic 2008, Fourth Edition 36
37. Coding the CD Collection Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 37
38. Coding the CD Collection Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 38
39. Coding the Form’s Load Event Procedure Programming with Microsoft Visual Basic 2008, Fourth Edition 39
40. Coding the Form’s Load Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 40
41. Coding the Form’s Load Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 41
42. Coding the btnAdd Control’s Click Event Procedure Programming with Microsoft Visual Basic 2008, Fourth Edition 42
43. 43 Programming with Microsoft Visual Basic 2005, Third Edition 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 Control’s Click Event Procedure Main task: Allow user to remove selected line from list box control
RemoveAt method: Removes list box item at specified index Programming with Microsoft Visual Basic 2008, Fourth Edition 44
45. Coding the btnRemove Control’s Click Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 45
46. Coding the Form’s FormClosing Event Procedure Programming with Microsoft Visual Basic 2008, Fourth Edition 46
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
Programming with Microsoft Visual Basic 2008, Fourth Edition 47