+ All Categories
Home > Documents > Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

Date post: 12-Jan-2016
Category:
Upload: cornelius-bryan
View: 218 times
Download: 0 times
Share this document with a friend
50
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling
Transcript
Page 1: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

Microsoft Visual Basic 2008

CHAPTER NINE

Using Arraysand File Handling

Page 2: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 2

Objectives

►Initialize an array

►Initialize an array with default values

►Access array elements using a loop

►Use ReDim [ Preserve ] to resize an array

Page 3: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 3

Objectives

►Determine the number of elements in an array using the Length property

►Use the For Each loop (read-only iteration)

►Initialize two-dimensional arrays

Page 4: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 4

Objectives

►Read a text file

►Write to a text file

►Calculate depreciation

►Use multiple Form objects

►Access Variable objects on other forms

Page 5: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 5

Introduction

Page 6: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 6

Introduction to Arrays

►An array is a collection of data of the same type. It is implemented in VisualBasic as an object.

►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 7: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 7

Declaring/Initializing an Array

300 is the upper bound of the array. This array actually contains 301 elements (zero-based indexing)

Page 8: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9 Example Array Declarations

Dim names(1) as String

names(0) = “Jack”

names(1) = “Marge”

Dim names( ) as String = {"Jack", "Marge"}

Dim names as String( )

names = New String() {"Jack", "Marge"}

-------------------------------------------------------------------------------------------------

MsgBox(names(0), , "First Name")

MsgBox(names(1), , "Second Name")

MsgBox("Array size for names is " & names.Length, , "Array Size")

Chapter 9: Using Arrays and File Handling 8

Page 9: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 9

Default Data Types

Data Type Default Value

Any numeric data type 0

Reference data type Null

Boolean data type False

Page 10: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 10

Initializing an Array

►Parallel arrays store related data in two or more arrays

Page 11: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 11

Accessing Array Elements Using a Loop

Page 12: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 12

Introduction to Arrays

► The Visual Basic compiler determines if each subscript is within the boundaries set when you initialized the array (bounds checking). If a subscript is out of range an IndexOutOfRangeException exception is generated.

► An array may be declared using 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 ReDim arrayX(50)

• 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 arrayX(100)

Page 13: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 13

Using the Length Property

►The Length property of an array contains the number of elements in an array (1 less than upper bound)

Page 14: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 14

Using Arrays

Page 15: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 15

The For Each Loop

Page 16: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 16

The For Each Loop

Page 17: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 17

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 18: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 18

Passing an Array [ pass a reference… ]

►An array can be passed as an argument to a Sub procedure or a Function procedure ( book page 666 )

Page 19: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 19

Sorting an Array [ Use Array class ]

Page 20: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 20

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 21: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 21

Creating a Two-Dimensional Array

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

► A two-dimensional array can also be described as an “array of arrays”.

Page 22: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 22

Creating a Two-Dimensional Array

Page 23: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 23

File Handling

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

►Text files have an extension that ends in .txt

►A simple text file is called a sequential file (actually it is usually called an ascii text file)

Page 24: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 24

Reading a Text File

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

Page 25: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 25

File Processing Classes

►System.IO namespace File Processing Classes

►File [ open, close, exists, … ]

►BinaryReader [ binary files ]►BinaryWriter

►StreamReader [ text files ]►StreamWriter

Page 26: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 26

Reading a Text File

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

Page 27: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 27

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.

► Create a StreamReader object

► Open the text file and assign the reference to the StreamReader object

Declare 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

Page 28: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 28

Reading a Text File

Dim objReader as IO.StreamReader

If IO.File.Exists(“e:\inventory.txt”) Then

objReader = IO.File.OpenText(“e:\inventory.txt”)

Else

MsgBox(“The file is not available.”, , “Error”)

Me.close()

End If

Dim ObjReader as New IO.StreamReader( “…” )

Page 29: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 29

Reading a Text File

►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 30: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 30

Reading a Text File

Page 31: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 31

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 class which is used to write a stream of text to a file

Page 32: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 32

Writing to a Text File

Page 33: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 33

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

Page 34: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 34

Computing Depreciation

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

Page 35: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 35

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 2008 window named frmDisplayInventory.vb.

In the Properties window, change the Text property of the frmDisplayInventory object to Sorted Inventory Listing

Page 36: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 36

Using Multiple Form Objects

Page 37: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 37

Startup Objects►Every application begins executing a project by displaying

the object designated as the Startup object

Page 38: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 38

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 [ ShowDialog( ) ]

• A modeless form allows you to switch the input focus to another window [ Show( ) ]

Page 39: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 39

Creating an Instance of a Windows Form Object

Page 40: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 40

Accessing Variables on Other Forms

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

Page 41: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9 Application Class

►If you have an application with multiple forms (windows), you can exit the “application” and close all the open forms (windows) by using the Exit method of the Application Class.

►Application.Exit( )

Chapter 9: Using Arrays and File Handling 41

Page 42: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 42

Page 43: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 43

Program Design

Page 44: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 44

Designing the Program Processing Objects

Page 45: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 45

Designing the Program Processing Objects

Page 46: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 46

Designing the Program Processing Objects

Page 47: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 47

Summary

►Initialize an array

►Initialize an array with default values

►Access array elements using a loop

►Use ReDim to resize an array

Page 48: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 48

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

Page 49: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

9

Chapter 9: Using Arrays and File Handling 49

Summary

►Write to a text file

►Calculate depreciation

►Use multiple Form objects

►Access Variable objects on other forms

Page 50: Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.

Microsoft Visual Basic 2008

CHAPTER 9 COMPLETE

Using Arraysand File Handling


Recommended