Using Arrays and File Handling

Slides:



Advertisements
Similar presentations
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

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.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 8 Arrays and Strings
Chapter 7 - Visual Basic Schneider
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
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.
Enhancing User Interaction Through Programming
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 8 Arrays and Strings
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
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.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
© 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.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
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.
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.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
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 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 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
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
Chapter 2: The Visual Studio .NET Development Environment
Files.
Using Procedures and Exception Handling
Variables and Arithmetic Operations
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Using Arrays and File Handling CHAPTER EIGHT Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

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

Introduction Chapter 8: Using Arrays and File Handling

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

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

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

Accessing Array Elements Using a Loop Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

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

Using Arrays Chapter 8: Using Arrays and File Handling

The For Each Loop Chapter 8: Using Arrays and File Handling

The For Each Loop Chapter 8: Using Arrays and File Handling

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

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

Sorting an Array Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

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

Creating a Two-Dimensional Array Chapter 8: Using Arrays and File Handling

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

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

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

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 Chapter 8: Using Arrays and File Handling

Reading a Text File Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

Reading a Text File Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

Writing to a Text File Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling

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

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 Chapter 8: Using Arrays and File Handling

Using Multiple Form Objects Chapter 8: Using Arrays and File Handling

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

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 Chapter 8: Using Arrays and File Handling

Creating an Instance of a Windows Form Object Chapter 8: Using Arrays and File Handling

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

Program Design Chapter 8: Using Arrays and File Handling

Program Design Chapter 8: Using Arrays and File Handling

Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling

Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling

Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling

Designing the Program Processing Objects Chapter 8: Using Arrays and File Handling

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 Chapter 8: Using Arrays and File Handling 43

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 44

CHAPTER EIGHT COMPLETE Using Arrays and File Handling