Microsoft Visual Basic 2015 CHAPTER EIGHT Using Arrays

Microsoft Visual Basic 2015 CHAPTER EIGHT Using Arrays and File Handling

8 Objectives ►Initialize an array with default values ►Access array elements using a loop ►Use Re. Dim to resize an array ►Determine the number of elements in an array using the Length command ►Use the For Each loop Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3

8 Introduction Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6

8 Initializing an Array ►Parallel arrays store related data in two or more arrays Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7

8 Accessing Array Elements Using a Loop Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8

8 Array Boundaries ► 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 its upperbound index ► Every array in Visual Basic is considered dynamic, which means that you can resize it at run time ► The Re. Dim 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: Re. Dim Preserve Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9

8 Using the Length Property ►The Length property of an array contains the number of elements in an array Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10

8 Using Arrays Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11

8 The For Each Loop Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12

8 The For Each Loop Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14

8 Passing an Array ►An array can be passed as an argument to a Sub procedure or a Function procedure Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15

8 Sorting an Array Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16

8 Searching an Array ►Searching each element in an array is called a sequential search ►The Binary. Search 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17

8 Creating a Two-Dimensional Array ►A two-dimensional array holds data that is arranged in rows and columns Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18

8 Creating a Two-Dimensional Array Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20

8 Reading a Text File ►To open a text file, you need an object available in the System. IO called a Stream. Reader Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21

8 Reading a Text File ►To determine whether the end of the file has been reached, use the Peek procedure of the Stream. Reader object Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22

8 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 frm. Depreciation_Load event ► Initialize the variables. Assign an object variable to the IO. Stream. Reader object. Initialize the Stream. Reader object by typing Dim obj. Reader As IO. An Intelli. Sense window opens. Select Stream. Reader. 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 Intelli. Sense window. Complete the rest of the line using Intelli. Sense as shown on the following slide. Assign the obj. Reader variable by typing obj. R and then pressing CTRL + SPACEBAR to complete the variable name. Type = IO. and Intelli. Sense opens. Type F Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23

8 Reading a Text File Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 24

8 Reading a Text File ► Select File by typing a period and select Open. Text from the Intelli. Sense 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 Read. Line() procedure reads each line of the text file. Use the variable int. Count 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25

8 Reading a Text File Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26

8 Writing to a Text File ►Writing to a text file is similar to reading a text file. The System. IO namespace also includes the Stream. Writer, which is used to write a stream of text to a file Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27

8 Writing to a Text File Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28

8 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 it can reasonably be expected to benefit the company, which is called the life of the asset Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29

8 Computing Depreciation ►The double-declining balance depreciation method is like the straight-line method doubled Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30

8 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 frm. Display. Inventory. vb in the Name text box ► Click the Add button in the Add New Item dialog box. A second Form object named frm. Display. Inventory. vb opens in the Visual Basic 2015 window. In the Properties window, change the Text property of the frm. Display. Inventory object to Sorted Inventory Listing Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31

8 Using Multiple Form Objects Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32

8 Startup Objects ►Every application begins executing a project by displaying the object designated as the Startup object Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33

8 Creating an Instance of a Windows Form Object ► To display a second or subsequent form, the initial step 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34

8 Accessing Variables on Other Forms ►You control the availability of a variable by specifying its access level, or access specifier Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35

8 Program Design Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36

8 Program Design Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 37

8 Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38

8 Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 39

8 Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 40

8 Summary ►Initialize an array with default values ►Access array elements using a loop ►Use Re. Dim to resize an array ►Determine the number of elements in an array using the Length command ►Use the For Each loop Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 41

8 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 Chapter 8: Using Arrays and File Handling © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 42

Microsoft Visual Basic 2015 CHAPTER EIGHT COMPLETE Using Arrays and File Handling
- Slides: 43