microsoft visual basic 2012 chapter eight using arrays and file handling

45
Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

Upload: jeffrey-black

Post on 28-Dec-2015

243 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

Microsoft Visual Basic 2012

CHAPTER EIGHT

Using Arraysand File Handling

Page 2: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 2

Objectives

►Initialize an array►Initialize an array with default values►Access array elements using a loop►Use ReDim to resize an array►Determine the number of elements in an array

using the Length command►Use the For Each loop

Page 3: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 3

Objectives

►Initialize two-dimensional arrays►Read a text file►Write to a text file►Calculate depreciation►Use multiple Form objects►Access Variable objects on other forms

Page 4: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 4

Introduction

Page 5: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 5

Introduction to Arrays

►An array variable is simply a variable that can store more than one value

►Each individual item in array that contains a value is called an element

►Arrays provide access to data by using a numeric index, or subscript, to identify each element in the array

Page 6: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 6

Initializing an Array

►To declare an array in a program, you must include an array declaration statement, which states the name of the array, how many items it can store, and what sort of data it can store

Page 7: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 7

Initializing an Array

►Parallel arrays store related data in two or more arrays

Page 8: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 8

Accessing Array Elements Using a Loop

Page 9: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 9

Introduction to Arrays

►The Visual Basic compiler determines if each subscript is within the boundaries set when you initialized the array

►An array can use a constant value representing the upper-bound index

►Every array in Visual Basic is considered dynamic, which means that you can resize it at run time

►The ReDim statement assigns a new array size to the specified array variable• All data in the array will be lost

►If you want to preserve the existing data you can use the keyword Preserve• Ex: ReDim Preserve

Page 10: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 10

Using the Length Property

►The Length property of an array contains the number of elements in an array

Page 11: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 11

Using Arrays

Page 12: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 12

The For Each Loop

Page 13: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 13

The For Each Loop

Page 14: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 14

Scope of Arrays

►The scope of an array declared within a procedure is local to that procedure, but an array can be declared as a class level variable

Page 15: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 15

Passing an Array

►An array can be passed as an argument to a Sub procedure or a Function procedure

Page 16: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 16

Sorting an Array

Page 17: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 17

Searching an Array

►Searching each element in an array is called a sequential search

►The BinarySearch method searches a sorted array for a value using a binary search algorithm• The binary search algorithm searches an array

by repeatedly dividing the search interval in half

Page 18: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 18

Creating a Two-Dimensional Array

►A two-dimensional array holds data that is arranged in rows and columns

Page 19: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 19

Creating a Two-Dimensional Array

Page 20: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 20

File Handling

►To process data more efficiently, many developers use text files to store and access information to use within an application

►Text files have a .txt extension►A simple text file is called a sequential file

Page 21: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 21

Reading a Text File

►To open a text file, you need an object available in the System.IO called a StreamReader

Page 22: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 22

Reading a Text File

►To determine whether the end of the file has been reached, use the Peek procedure of the StreamReader object

Page 23: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 23

Reading a Text File

►Open the code window by tapping or clicking the View Code button on the Solution Explorer toolbar. Tap or click inside the frmDepreciation_Load event

►Initialize the variables. Assign an object variable to the IO.StreamReader object. Initialize the StreamReader object by typing Dim objReader As IO. An IntelliSense window opens. Select StreamReader. Press ENTER. Finish declaring the rest of the variable names

►Verify that the inventory.txt data file is available by typing If IO. to open an IntelliSense window. Complete the rest of the line using IntelliSense as shown on the following slide. Assign the objReader variable by typing objR and then pressing CTRL + SPACEBAR to complete the variable name. Type = IO. and IntelliSense opens. Type F

Page 24: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 24

Reading a Text File

Page 25: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 25

Reading a Text File

►Select File by typing a period and select OpenText from the IntelliSense list. Type (“e:\inventory.txt”) to access the inventory text file from the USB drive (drive E)

►To read each line of the text file, insert a Do While loop that continues until the Peek procedure returns the value of -1. Specify that the ReadLine() procedure reads each line of the text file. Use the variable intCount to determine the index of each array element

►After the data file has been read, close the file. Insert an Else statement that informs the user if the file cannot be opened and closes the application

Page 26: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 26

Reading a Text File

Page 27: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 27

Writing to a Text File

►Writing to a text file is similar to reading a text file. The System.IO namespace also includes the StreamWriter, which is used to write a stream of text to a file

Page 28: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 28

Writing to a Text File

Page 29: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 29

Computing Depreciation

►Depreciation is the decrease in property value and the reduction in the balance sheet value of a company asset to reflect its age and prolonged use

►The simplest and most common method, straight-line depreciation, is calculated by dividing the purchase or acquisition price of an asset by the total productive years the asset can reasonably be expected to benefit the company, which is called the life of the asset

Page 30: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 30

Computing Depreciation

►The double-declining balance depreciation method is like the straight-line method doubled

Page 31: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 31

Using Multiple Form Objects

►Tap or click PROJECT on the menu bar, and then tap or click Add Windows Form

►In the Add New Item dialog box, tap or click Windows Form, and then type frmDisplayInventory.vb in the Name text box

►Click the Add button in the Add New Item dialog box. A second Form object named frmDisplayInventory.vb opens in the Visual Basic 2012 window. In the Properties window, change the Text property of the frmDisplayInventory object to Sorted Inventory Listing

Page 32: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 32

Using Multiple Form Objects

Page 33: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 33

Startup Objects

►Every application begins executing a project by displaying the object designated as the Startup object

Page 34: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 34

Creating an Instance of a Windows Form Object

►To display a second or subsequent form, the initial step in displaying the form is to create an instance of the Windows Form object

►When creating multiple Windows Form objects, Visual Basic allows you to generate two types of forms: modal and modeless• A modal form retains the input focus while

open• A modeless form allows you to switch the input

focus to another window

Page 35: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 35

Creating an Instance of a Windows Form Object

Page 36: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 36

Accessing Variables on Other Forms

►You control the availability of a variable by specifying its access level, or access specifier

Page 37: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 37

Program Design

Page 38: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 38

Program Design

Page 39: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 39

Designing the Program Processing Objects

Page 40: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 40

Designing the Program Processing Objects

Page 41: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 41

Designing the Program Processing Objects

Page 42: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 42

Designing the Program Processing Objects

Page 43: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 43

Summary

►Initialize an array►Initialize an array with default values►Access array elements using a loop►Use ReDim to resize an array►Determine the number of elements in an array

using the Length command►Use the For Each loop

Page 44: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

8

Chapter 8: Using Arrays and File Handling 44

Summary

►Initialize two-dimensional arrays►Read a text file►Write to a text file►Calculate depreciation►Use multiple Form objects►Access Variable objects on other forms

Page 45: Microsoft Visual Basic 2012 CHAPTER EIGHT Using Arrays and File Handling

Microsoft Visual Basic 2012

CHAPTER EIGHT COMPLETE

Using Arraysand File Handling