1 / 54

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Microsoft Visual Basic 2010: Reloaded Fourth Edition. Chapter Eleven Structures and Sequential Files. 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

sthoms
Download Presentation

Microsoft Visual Basic 2010: Reloaded Fourth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Microsoft Visual Basic 2010: ReloadedFourth 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 form’s Declarations section Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  5. Structures (cont'd.) Figure 11-1: How to define a structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  8. Declaring a Structure Variable (cont'd.) Figure 11-3: How to use a member variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  9. Passing a Structure Variable to a Procedure • Sample application without using a structure: Figure 11-4: Sample run of the Willow Pools application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  10. Figure 11-5: Code for the Willow Pools application (without a structure) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  11. Figure 11-6: Code for the Willow Pools application (with a structure) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  14. Figure 11-8: Partial code for the Treasures Gift Shop application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  15. Creating an Array of Structure Variables (cont’d.) Figure 11-8: Partial code for the Treasures Gift Shop application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  18. Writing Data to a Sequential Access File (cont'd.) Figure 11-9: How to declare a StreamWriter variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  20. Figure 11-10: How to create a StreamWriter object Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  22. Writing Data to a Sequential Access File (cont'd.) Figure 11-11: How to write to a sequential access file Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  24. Closing an Output Sequential Access File (cont’d.) Figure 11-12: How to close an output sequential access file Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  26. Reading Data from a Sequential Access File (cont'd.) Figure 11-13: How to declare a StreamReader variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  27. Reading Data from a Sequential Access File (cont'd.) Figure 11-14: How to create a StreamReader object Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  29. Reading Data from a Sequential Access File (cont'd.) Figure 11-15: How to determine whether a sequential access file exists Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  31. Reading Data from a Sequential Access File (cont'd.) Figure 11-16: How to read data from a sequential access file Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  33. Reading Data from a Sequential Access File (cont'd.) Figure 11-17: How to use the Peek method Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  36. Figure 11-20: Partial code for the Game Show Contestants application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  37. The Game Show Contestants Application (cont’d.) Figure 11-20: Partial code for the Game Show Contestants application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  39. Figure 11-22: How to align columns of information Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  40. Aligning Columns of Information (cont'd.) Figure 11-22: How to align columns of information (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  42. The FormClosing Event (cont'd.) Figure 11-23: How to use the FormClosing event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  43. The FormClosing Event (cont'd.) Figure 11-23: How to use the FormClosing event procedure (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  45. Figure 11-24: How to write records to a sequential access file Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  46. Writing and Reading Records (cont’d.) • 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  47. Writing and Reading Records (cont’d.) Figure 11-25: How to read records from a sequential access file Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  48. Writing and Reading Records (cont’d.) Figure 11-25: How to read records from a sequential access file (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  50. Programming Tutorial 2 • Creating the CD Collection Application Figure 11-30: MainForm for the CD Collection application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

More Related