CIS16 Application Development and Programming using Visual Basic.net Chapter Nine Structures and Sequential Files
STRUCTURES
Structures Structures are objects you can create to hold data or code Used when you need to pass a group of related items to a procedure for further processing, or to store related items in an Array Structure statement is defined in the Declarations section of the class –NOT inside a SUB
Structures Like an Array Like a record in a table in that related items are grouped into list Like a record in a table in that related items are grouped together with a unique identifier
Structures Inside the structure are the items (members) Items can have different data types, and can be variables, constants or procedures Use Pascal case when defining items
Structures (cont'd.) Variables Referred to as Member Variables Each member variable’s definition contains the keyword Public followed by the variable’s name and data type
Structures (cont'd.) Structures can be instantiated into structure variable objects structure variable is an object declared using a structure as its data type rather than a true data type If declaring a structure variable object in a procedure Use the Dim keyword If declaring a structure variable object in a Class use the Private keyword
Structures (cont'd.) Instantiating
Structures (cont'd.) Populating
Creating an Array of Structure Variables
Creating an Array of Structure Variables a structure variable can be stored in an array, even when its members have different data types
Sequential Access Files
Reading and Writing Records Sequential files can be used to store records and fields A field is a single item of information about a person, place, or thing A record is a group of related fields that contain all of the necessary data about a specific person, place, or thing When writing records to a sequential access file, programmers typically write each record on a separate line in the file They use a special character, called a delimiter character, to separate each field Commonly used delimiter characters include the comma and the number (or hash) sign (#)
Sequential Access Files Input files An application uses the data in these files as input Output files Files that store the output produced by an application Sequential access files Composed of lines of text that are both read and written sequentially (in consecutive order), one line at a time Also called text files
Sequential Access File Objects StreamWriter object Allows you to write a stream of characters to a sequential access file StreamReader object Used to read data streams from a sequential access file
STREAMWRITER Methods: CreateText() AppendText() Write() WriteLine() Close() STREAMWRITER
StreamWriter Used to write data to a sequential access file StreamWriter object Allows you to write a stream of characters to a sequential access file Define Writer as procedure/class level object
StreamWriter Used to write data to a sequential access file CreateText method Used to open a sequential access file for output (creates a new, empty file) Unless otherwise specified, gets created in the bin/Debug folder
StreamWriter Used to write data to a sequential access file Write method When using the Write method, the next character written to the file will appear immediately after the last letter in the string WriteLine method When using the WriteLine method, the next character written to the file will appear on the line immediately below the string
StreamWriter Used to write data to a sequential access file AppendText method Used to open a sequential access file for append(new data is written after existing data)
StreamWriter Used to write data to a sequential access file Use the Close method to close an output sequential access file Ensures that the data is saved and it makes the file available for use elsewhere in the application
Writing Records (cont'd.) When writing records, separate fields using a delimiter
Methods: OpenText() Peek() ReadLine() Close() STREAMREADER
StreamReader Used to read data from a sequential access file Define StreamReader as a procedure/class level object(s)
StreamReader Used to read data from a sequential access file Before using it, use EXISTS method to make sure it actually was created Exists method Returns the Boolean value True if the file exists; otherwise, it returns the Boolean value False
StreamReader Used to read data from a sequential access file The OpenText method opens a sequential access file for input, and creates an instance of the Reader Unless otherwise specified, looks for it in the bin/Debug folder
StreamReader Used to read data from a sequential access file Peek method “Peeks” into the file to determine whether it contains another line to read -1 indicates EOF
StreamReader Used to read data from a sequential access file The ReadLine method reads the file’s contents, one line (characters followed by the newline character) at a time
StreamReader Used to read data from a sequential access file The Close method closes the file
Reading Records To Read records with multiple fields, use Split function Separates fields in record, creating a String array Specify delimiter, and data type conversion, if necessary
Adding a Menu to a Form Menu strip control Used to include one or more menus on a form A menu title appears on the menu bar at the top of the form Menu items can include: Commands Submenu items Separator bars Clicking a command on a menu executes it Clicking a submenu item opens an additional menu Separator bars provide visual grouping
Adding a Menu to a Form (cont.) Menu title captions should be one word only Menu item captions can be from one to three words Assign unique access keys to menu titles and items Follow Windows menu standards: Use book title capitalization for menu item captions An ellipsis (…) after an item caption indicates a menu item requires more information The FILE menu should be the first item on the menu bar Cut, Copy, and Paste should appear on the EDIT menu
Adding a Menu to a Form Menu Strip Control To include one or more menus on a Form The shortcut keys appear to the right of a menu item and allow the user to select the item without opening the menu
Adding a Menu to a Form (cont'd.) Figure 10-9: FILE menu
Adding a Menu to a Form (cont.) Assigning Shortcut Keys to Menu Items Shortcut keys Appear to the right of a menu item Allow the user to select an item without opening a menu Example: Ctrl+X and Ctrl+V Cut and paste commands Assign shortcut keys to commonly used menu items Follow Windows standard conventions Shortcut keys can be used when a menu is closed
Adding a Menu to a Form (cont.) Assigning Shortcut Keys to Menu Items (cont.) Figure 8-28 Location of the shortcut keys on the menu Figure 8-27 Shortcut keys specified in the ShortcutKeys box
Adding a Menu to a Form (cont.) Coding the Exit Menu Item How to end the Pizza Game application: The user clicks the Exit item on the FILE menu The Exit item’s Click event procedure calls Me.Close() Coding the txtLetter Control’s KeyPress Event Figure 8-29 txtLetter_KeyPress procedure