1 / 30

Programming with Microsoft Visual Basic 2010 5 th Edition

Programming with Microsoft Visual Basic 2010 5 th Edition. Chapter Ten Structures and Sequential Access Files. 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

Download Presentation

Programming with Microsoft Visual Basic 2010 5 th 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. Programming with Microsoft Visual Basic 2010 5th 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

  3. Previewing the CD Collection Application (cont’d.) Figure 10-1 CD information added to the list box

  4. Previewing the CD Collection Application (cont’d.) 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 and use a structure variable • Pass a structure variable to a procedure • Create an array of structure variables

  6. Structures • 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

  7. Structures (cont’d.) 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 hourly As Employee • hourly is variable declared with Employee structure type • Accessing member variable in code • Use structureVariableName. memberVariableName • Example: hourly.dblPay = 26 • Member variables are used like scalar variables

  9. Declaring and Using a Structure Variable (cont’d.) Figure 10-4 Syntax and examples of declaring a structure variable

  10. Figure 10-5 Examples of using a member variable

  11. Passing a Structure Variable to a Procedure • Application for sales manager at Willow Pools • Allows user to enter length, width, and depth • Calculates gallons of water to fill 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. Figure 10-7 Code for the Willow Pools application (without a structure)

  13. Figure 10-8 Code for the Willow Pools application (with a structure)

  14. Lesson A Summary • Structures: User-defined data types • Structure members can be variables, constants, or procedures • Refer to member within structure variable using structureVariableName.memberVariableName • To create an array of structure variables: • Declare array using structure as data type • Refer to member within structure variable stored in an array using: arrayName(subscript).memberVariableName

  15. 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

  16. Sequential Access Files • Reading a file: Getting data from a file • Writing to a file: Sending data to a file • Output files: Store application output • Input files: Application uses data in these files • Sequential access files • Composed of lines of text that are both read and written in consecutive order • Also called text files

  17. 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 application • Uses StreamWriter variable

  18. Writing Data to a Sequential Access File (cont’d.) Figure 10-15 Syntax and an example of declaring a StreamWriter variable

  19. Figure 10-17 Syntax and examples of the CreateText and AppendText methods

  20. Figure 10-18 Syntax and examples of the Write and WriteLine methods

  21. Closing an Output Sequential Access File • Close method • Used to close an output sequential access file Figure 10-19 Syntax and an example of closing an output sequential access file

  22. 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

  23. Figure 10-21 Syntax and an example of declaring a StreamReader variable

  24. Figure 10-22 Syntax and an example of the OpenText method

  25. Figure 10-23 Syntax and an example of the Exists method

  26. Figure 10-24 Additional code entered in the procedure

  27. Reading Data from a Sequential Access File (cont’d.) • 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

  28. Figure 10-25 Syntax and an example of the ReadLine method

  29. Figure 10-26 Syntax and an example of the Peek method

  30. Closing an Input Sequential Access File • Close method • Used to close input sequential access files Figure 10-27 Syntax and an example of closing an input sequential access file

More Related