Download presentation
Presentation is loading. Please wait.
Published byΖεφύρα Σπηλιωτόπουλος Modified over 5 years ago
1
CIS16 Application Development and Programming using Visual Basic.net
Chapter Nine Structures and Sequential Files
2
STRUCTURES
3
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
4
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
5
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
6
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
7
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
8
Structures (cont'd.) Instantiating
9
Structures (cont'd.) Populating
10
Creating an Array of Structure Variables
11
Creating an Array of Structure Variables
a structure variable can be stored in an array, even when its members have different data types
12
Sequential Access Files
13
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 (#)
14
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
15
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
16
STREAMWRITER Methods: CreateText() AppendText() Write() WriteLine()
Close() STREAMWRITER
17
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
18
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
19
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
20
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)
21
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
22
Writing Records (cont'd.)
When writing records, separate fields using a delimiter
23
Methods: OpenText() Peek() ReadLine() Close() STREAMREADER
24
StreamReader Used to read data from a sequential access file
Define StreamReader as a procedure/class level object(s)
25
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
26
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
27
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
28
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
29
StreamReader Used to read data from a sequential access file
The Close method closes the file
30
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
31
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
32
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
33
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
34
Adding a Menu to a Form (cont'd.)
Figure 10-9: FILE menu
35
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
36
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
37
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.