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.

Slides:



Advertisements
Similar presentations
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 15 Creating Database Forms and Reports Introduction Forms Reports.
Chapter 5: Database Forms and Reports
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
VBA Modules, Functions, Variables, and Constants
C++ for Engineers and Scientists Third Edition
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Lesson 9: Fine-Tuning Your Workflow Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Lesson 10: Adding Flash, Video, and Sound Content Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver.
Lesson 11: Maximizing Site Design Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
Lesson 13: Building Web Forms Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
9-1 Chapter 9 Working with Databases in VB.NET. 9-2 Learning Objectives Understand how databases are used to store business data and how they differ from.
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Introduction to Databases Chapter 7: Data Access and Manipulation.
IE 212: Computational Methods for Industrial Engineering
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.
Using Arrays and File Handling
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
7 - 1 Chapter 7: Data Analysis for Modeling PowerPoint Slides Prepared By: Alan Olinsky Bryant University Management Science: The Art of Modeling with.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
© 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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
4 - 1 Chapter 4: Advanced Excel Skills Management Science: The Art of Modeling with Spreadsheets, 3e S.G. Powell K.R. Baker © John Wiley and Sons, Inc.
Chapter Six: Working With Arrays in Visual Basic.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Lesson 8: Working with Tables Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
13-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Copyright © 2000 John Wiley & Sons, Inc. All rights reserved
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
9-1 Iteration: Beyond the Basic PERFORM Chapter 9.
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Copyright © 2000 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976.
7-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Copyright 2006 John Wiley & Sons, Inc. Beni Asllani University of Tennessee at Chattanooga Operations Management - 5 th Edition Chapter 10 Supplement Roberta.
Slide 6-1 Chapter 6 System Software Considerations Introduction to Information Systems Judith C. Simon.
Slide 8-1 Chapter 8 Terms Programming Languages Introduction to Information Systems Judith C. Simon.
Slide 1 Systems Analysis and Design with UML Version 2.0, Second Edition Alan Dennis, Barbara Wixom, and David Tegarden Chapter 9: Moving on to Design.
A- 1. A- 2 Appendix B Standards of Ethical Conduct for Management Accountants The Institute of Management Accountants has published and promoted the following.
K-1. K-2 Appendix K Standards of Ethical Conduct for Management Accountants The Institute of Management Accountants has published and promoted the following.
Slide 11-1 Chapter 11 Terms Information Resource Management Strategies Introduction to Information Systems Judith C. Simon.
H-1 Prepared by Coby Harmon University of California, Santa Barbara Westmont College.
2-1 Chapter 2 Using VB.NET to Create a First Solution.
“Copyright © 2001 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976.
Programming Right from the Start with Visual Basic .NET 1/e
An Introduction to Programming and VB.NET
Chapter 3.5 Input and Output
Input and Output.
Input and Output Chapter 3.5
Introduction to Computer Programming IT-104
Presentation transcript:

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 between arrays and combo boxes, list boxes, and similar controls. Declare the maximum index value for a list array and understand the errors that occur when declared upper limits on index values are exceeded. Input data into an array from a file using loops and the StreamReader object and redimension it to match the number of elements input.

6-3 Learning Objectives (continued) Work with array data to find the sum and average of array values, the largest or smallest value in an array, match values in one array to those in another, and find a particular value in an array. Add forms to a project and display or hide them. Declare, input, process, and output two-dimensional arrays. Use Step operations to step though an array operation to find and correct an error.

6-4 Working with lists as arrays To store the data in memory as a list or table, use an array. Arrays provide a way of working with long lists in memory as if working with shorter lists using list and combo box controls.

6-5 Lists stored as arrays vs Lists stored as controls List and combo boxes – Respond to control properties like Sorted (arrays do not) – Have Items.Count property (arrays do not) – Store only text in the form of character strings Arrays – Can store any type of data – Can store only one type of data at a time – It can store String data or Decimal data, but not both.

6-6 Accessing members of an array To access an item one needs two things: 1. The name of the array 2. The position of the item in the array (its index) – The index must be an integer constant, variable, or expression. Example: x(3) is the fourth element of the array named x

6-7 Array Declaration You must declare an array for it to be used in a project. – Dim ArrayName(max_index_value) as variable_type – ArrayName is the name of the array – max_index_value is the maximum number of elements in array – variable_type is the type of each element of the array The same variables scopes are available to array variables, – Form level – Local/Procedure level – Block level – Static

6-8 Resizing arrays By default, arrays are of fixed size, the same size as when declared Arrays can be resized using the ReDim statement When using ReDim, the original array is destroyed and replaced with a new one with the new dimension To keep the values already in array, use the Preserve keyword

6-9 ReDim : Syntax (1/2) Declare an array then use ReDim Dim x() as Integer ReDim x(2) x(0)=0 x(1)=1 ReDim x(20) MsgBox(x(1)) ‘ 0 is displayed because array was ‘ destroyed when using ReDim

6-10 ReDim : Syntax (2/2) Declare an array then use ReDim Dim x() as Integer ReDim x(2) x(0)=0 x(1)=1 ReDim Preserve x(20) MsgBox(x(1)) ‘ 1 is displayed because array was ‘ copied when using ReDim Preserve

6-11 Inputting values to an array General form of loop to input from a file Dim ArrayName() as data type Dim intCounter as Integer intCounter = 0 Do Until EOF( n) Redim Preserve ArrayName(intCounter) Input n, ArrayName(intCounter) intCounter = intCounter + 1 Loop

6-12 For-Next and For-Each loops If size of array is known use For-Next For intCounter = 0 to 100 sngScores(intCounter) = -1.0 Next If size of array is not known use For-Each Dim sngValue as Single For Each sngValue in sngScores sngValue = -1.0 Next

6-13 Step-by-Step 6-1: Inputting and Displaying an array Demo

6-14 Dealing with characters: The Chr() and Asc() functions ASCII characters are stored as an integer between 0 and 255 Chr(n) returns the character corresponding to the integer n – Chr(40) is “(“ Asc(char) returns the integer corresponding to the character char – Asc(“(“) is 40

6-15 Finding largest value in an array: Pseudocode Begin procedure to find largest value Set largest value to first item in list Repeat beginning with second item to last item If item in list > largest value then Largest value = item in list End decision End repeat Display largest value End procedure

6-16 Step-by-Step 6-2: Processing Arrays Demo

6-17 Finding items and working with multiple lists Each price in list associated with a part ID Objective of the project 1. Find the part with the maximum price and display the part ID and price 2. Find the part with a specified ID and display the part ID and price; if the part is not on the list, display a message. The input: a file with lines containing ID, Price for each part

6-18 Using StreamReader Class Allows to read an entire line from a file One needs to parse the result after First, one needs to – Declare a variable of type IO.StreamReader Dim sr As IO.StreamReader – Create an instance of IO.StreamReader sr= New IO.StreamReader(FileName) – Call methods of that instance sr.Peek()

6-19 Methods of StreamReader Peek(): returns -1 if at the end of stream Close(): releases all resources Read(): reads the whole stream Readline(): reads only one line

6-20 Parsing input: The Split Function Usually a line of entry will contain a number of parts separated by a common character (space, comma, tab …) The Split() function allows to “split” the whole line into its individual parts The Split() function returns an array of Strings

6-21 Split() examples Split(“This is this, and that is that”, “ “) returns the array {“This”, “is”, “this,”, “and”, “that”, “is”, “that” } Split(“This is this, and that is that”, “,“) returns the array {“This is this”, “and that is that” }

6-22 StreamReader Example Code: Form_Load Procedure Private Sub frmMultiple_Load(ByVal sender As System.Object, BbyVal e As System.EventArgs)_ Handles MyBase.Load Dim strPathName As String, strLine As String Dim chrDelimiter As Char, strFields() As String intNumPrices = 0 chrDelimiter = "," strPathName = CurDir() + "\Parts.txt" Dim srdReadFile As System.IO.StreamReader = New System.IO.StreamReader(strPathName) Do Until srdReadFile.Peek = -1 ReDim Preserve decPrices(intNumPrices) ReDim Preserve strPartID(intNumPrices) strLine = srdReadFile.ReadLine strFields = strLine.Split(chrDelimiter) intNumPrices = intNumPrices + 1 Loop FileClose() End Sub

6-23 The MsgBox() function, its parameters and its returned value You can use the second parameter of the MsgBox function to decide which button(s) will be displayed in the dialog box – MsgBox(..., vbOKOnly, …) You can find out which button of the dialog box was pressed before the code returned – MyReply = MsgBox( …, vbOKCancel, …)

6-24 MsgBox(): Constants and Values Returned

6-25 Step-by-Step 6-3: Working with multiple lists Demo

6-26 Adding another form to project Use the Project | Add Windows Form menu Enter a name in the dialog box displayed Add controls and code to the form

6-27 Creating and displaying another form Declare a reference to the new form – Dim frmForm2 As Form2 Create the form asset the reference to it – frmForm2 = New Form2() Display the form – frmForm2.Show() Access controls on the form – MsgBox(frmForm2.MyTextBox.Text)

6-28 Step-by-Step 6-4: Using Multiple Forms Demo

6-29 The InStr() function Use InStr() when you want to find a substring inside a string – InStr(“String to be searched”, “String to be found”) – commaLoc = InStr(“LastName,FirstName”, “,”) – commaLoc would be 8, the position of “,”

6-30 Step-by-Step 6-5: Searching for DVDs Demo

6-31 Two-Dimensional Arrays Declaration – Dim MyArray(RowCount, ColumnCount) As MyType You access each entry with two coordinates – MyArray(1,2) ‘element in second row, third column You can extend similarly to any number of dimensions you wish

6-32 Step-by-Step 6-6: Working with two-dimensional arrays Demo

6-33 Using step commands for debugging When in break mode, you can use the step commands to – Step Into: executes the active line of code – Step Over: same as Step Into except that if the line is a call to a procedure, the procedure is completely executed without stepping through – Step Out: Exits the innermost procedure in which the active line is

6-34 Step-by-Step 6-7: Use the Step commands in debugging. Demo

6-35 Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein