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

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Arrays
Advertisements

Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Programming with Microsoft Visual Basic 2005, Third Edition
Excel and VBA Creating an Excel Application
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
C++ for Engineers and Scientists Third Edition
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 8 Arrays and Strings
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
CHAPTER SIX.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Multiple Forms, Standard Modules, And Menus
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Using Arrays and File Handling
Chapter 8 Arrays and Strings
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Array Processing.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
1.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Programming with Microsoft Visual Basic th Edition
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Programming Right from the Start with Visual Basic .NET 1/e
VBA - Excel VBA is Visual Basic for Applications
Files.
Chapter 7 Arrays.
Using Procedures and Exception Handling
Variables and Arithmetic Operations
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Computer Programming IT-104
Presentation transcript:

Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling

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 [ Preserve ] to resize an array

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

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

9 Chapter 9: Using Arrays and File Handling5 Introduction

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

9 Chapter 9: Using Arrays and File Handling7 Declaring/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 ►When a number (size declarator) 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 when the array is created. 300 is the upper bound of the array. This array actually contains 301 elements (zero-based indexing)

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 Handling8

9 9 Default Data Types Data TypeDefault Value Any numeric data type0 Reference data typeNull Boolean data typeFalse

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

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

9 Chapter 9: Using Arrays and File Handling12 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)

9 Chapter 9: Using Arrays and File Handling13 Using the Length Property ►The Length property of an array contains the number of elements in an array (1 less than upper bound)

9 Chapter 9: Using Arrays and File Handling14 Using Arrays

9 Chapter 9: Using Arrays and File Handling15 The For Each Loop

9 Chapter 9: Using Arrays and File Handling16 The For Each Loop

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

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 )

9 Chapter 9: Using Arrays and File Handling19 Sorting an Array [ Use Array class ]

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

9 Chapter 9: Using Arrays and File Handling21 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”.

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

9 Chapter 9: Using Arrays and File Handling23 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)

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

9 Chapter 9: Using Arrays and File Handling25 File Processing Classes ►System.IO namespace File Processing Classes ►File [ open, close, exists, … ] ►BinaryReader [ binary files ] ►BinaryWriter ►StreamReader [ text files ] ►StreamWriter

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

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

9 Chapter 9: Using Arrays and File Handling28 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( “…” )

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

9 Chapter 9: Using Arrays and File Handling30 Reading a Text File

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

9 Chapter 9: Using Arrays and File Handling32 Writing to a Text File

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

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

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

9 Chapter 9: Using Arrays and File Handling36 Using Multiple Form Objects

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

9 Chapter 9: Using Arrays and File Handling38 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( ) ]

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

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

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 Handling41

9 Chapter 9: Using Arrays and File Handling42

9 Chapter 9: Using Arrays and File Handling43 Program Design

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

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

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

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

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

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

Microsoft Visual Basic 2008 CHAPTER 9 COMPLETE Using Arrays and File Handling