Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.

Similar presentations


Presentation on theme: "Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling."— Presentation transcript:

1 Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling

2 9 Chapter 9: Using Arrays and File Handling2 Objectives ►Initialize an array ►Initialize an array with default values ►Access array elements using a loop ►Use ReDim to resize an array

3 9 Chapter 9: Using Arrays and File Handling3 Objectives ►Determine the number of elements in an array using the Length command ►Use the For Each loop ►Initialize two-dimensional arrays ►Read a text file

4 9 Chapter 9: Using Arrays and File Handling4 Objectives ►Write to a text file ►Calculate depreciation ►Use multiple Form objects ►Access Variable objects on other forms

5 9 Chapter 9: Using Arrays and File Handling5 Introduction

6 9 Chapter 9: Using Arrays and File Handling6 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

7 9 Chapter 9: Using Arrays and File Handling7 Initializing an Array ►When a number is not used in the declaration statement to state the size of the array, the array is implicitly sized, meaning that the number of values is determined at execution

8 9 Chapter 9: Using Arrays and File Handling8 Initializing an Array ►Parallel arrays store related data in two or more arrays

9 9 Chapter 9: Using Arrays and File Handling9 Accessing Array Elements Using a Loop

10 9 Chapter 9: Using Arrays and File Handling10 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 of the array ►Every array in Visual Basic is considered dynamic, which means that you can resize the array 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

11 9 Chapter 9: Using Arrays and File Handling11 Using the Length Property ►The Length property of an array contains the number of elements in an array

12 9 Chapter 9: Using Arrays and File Handling12 Using Arrays

13 9 Chapter 9: Using Arrays and File Handling13 The For Each Loop

14 9 Chapter 9: Using Arrays and File Handling14 The For Each Loop

15 9 Chapter 9: Using Arrays and File Handling15 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

16 9 Chapter 9: Using Arrays and File Handling16 Passing an Array ►An array can be passed as an argument to a Sub procedure or a Function procedure

17 9 Chapter 9: Using Arrays and File Handling17 Sorting an Array

18 9 Chapter 9: Using Arrays and File Handling18 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

19 9 Chapter 9: Using Arrays and File Handling19 Creating a Two-Dimensional Array ►A two-dimensional array holds data that is arranged in rows and columns

20 9 Chapter 9: Using Arrays and File Handling20 Creating a Two-Dimensional Array

21 9 Chapter 9: Using Arrays and File Handling21 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 an extension that ends in.txt ►A simple text file is called a sequential file

22 9 Chapter 9: Using Arrays and File Handling22 Reading a Text File ►To open a text file, you need an object available in the System.IO called a StreamReader

23 9 Chapter 9: Using Arrays and File Handling23 Reading a Text File ►To determine whether the end of the file has been reached, use the Peek procedure of the StreamReader object

24 9 Chapter 9: Using Arrays and File Handling24 Reading a Text File ►Open the code editing window by clicking the View Code button on the Solution Explorer toolbar. 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. And 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. and an IntelliSense window opens. 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

25 9 Chapter 9: Using Arrays and File Handling25 Reading a Text File

26 9 Chapter 9: Using Arrays and File Handling26 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 21. 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

27 9 Chapter 9: Using Arrays and File Handling27 Reading a Text File

28 9 Chapter 9: Using Arrays and File Handling28 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

29 9 Chapter 9: Using Arrays and File Handling29 Writing to a Text File

30 9 Chapter 9: Using Arrays and File Handling30 Computing Depreciation ►Depreciation is the decrease in property value and the reduction in the balance sheet value of a company asset to reflect its loss of value through age and wear and tear ►The simplest and most common, 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

31 9 Chapter 9: Using Arrays and File Handling31 Computing Depreciation ►The double-declining balance depreciation method is like the straight-line method doubled

32 9 Chapter 9: Using Arrays and File Handling32 Using Multiple Form Objects ►In the Solution Explorer, right-click the project file name Depreciation. Point to Add on the shortcut menu, and then point to New Item on the submenu ►Click New Item. In the Add New Item dialog box, click Windows Form in the Templates area, 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 opens in the Visual Basic 2005 window named frmDisplayInventory.vb. In the Properties window, change the Text property of the frmDisplayInventory object to Sorted Inventory Listing

33 9 Chapter 9: Using Arrays and File Handling33 Using Multiple Form Objects

34 9 Chapter 9: Using Arrays and File Handling34 Startup Objects ►Every application begins executing a project by displaying the object designated as the Startup object

35 9 Chapter 9: Using Arrays and File Handling35 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

36 9 Chapter 9: Using Arrays and File Handling36 Creating an Instance of a Windows Form Object

37 9 Chapter 9: Using Arrays and File Handling37 Accessing Variables on Other Forms ►You control the availability of a variable by specifying its access level, or access specifier

38 9 Chapter 9: Using Arrays and File Handling38 Program Design

39 9 Chapter 9: Using Arrays and File Handling39 Program Design

40 9 Chapter 9: Using Arrays and File Handling40 Designing the Program Processing Objects

41 9 Chapter 9: Using Arrays and File Handling41 Designing the Program Processing Objects

42 9 Chapter 9: Using Arrays and File Handling42 Designing the Program Processing Objects

43 9 Chapter 9: Using Arrays and File Handling43 Summary ►Initialize an array ►Initialize an array with default values ►Access array elements using a loop ►Use ReDim to resize an array

44 9 Chapter 9: Using Arrays and File Handling44 Summary ►Determine the number of elements in an array using the Length command ►Use the For Each loop ►Initialize two-dimensional arrays ►Read a text file

45 9 Chapter 9: Using Arrays and File Handling45 Summary ►Write to a text file ►Calculate depreciation ►Use multiple Form objects ►Access Variable objects on other forms

46 Microsoft Visual Basic 2005 CHAPTER 9 COMPLETE Using Arrays and File Handling


Download ppt "Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling."

Similar presentations


Ads by Google