35
6-1 Chapter 6 Working with Arrays in VB .NET

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

Embed Size (px)

Citation preview

Page 1: 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

6-1

Chapter 6

Working with Arrays in VB .NET

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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.

Page 5: 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

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.

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

6-9

ReDim : Syntax (1/2)

Declare an array then use ReDimDim 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

Page 10: 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

6-10

ReDim : Syntax (2/2)

Declare an array then use ReDimDim 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

Page 11: 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

6-11

Inputting values to an array

General form of loop to input from a file Dim ArrayName() as data typeDim intCounter as IntegerintCounter = 0

Do Until EOF( n)Redim Preserve ArrayName(intCounter)Input n, ArrayName(intCounter)intCounter = intCounter + 1

Loop

Page 12: 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

6-12

For-Next and For-Each loops

If size of array is known use For-NextFor intCounter = 0 to 100

sngScores(intCounter) = -1.0

Next

If size of array is not known use For-EachDim sngValue as Single

For Each sngValue in sngScores

sngValue = -1.0

Next

Page 13: 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

6-13

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

Demo

Page 14: 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

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

Page 15: 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

6-15

Finding largest value in an array: Pseudocode

Begin procedure to find largest valueSet largest value to first item in list

Repeat beginning with second item to last itemIf item in list > largest value then

Largest value = item in listEnd decision

End repeat

Display largest valueEnd procedure

Page 16: 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

6-16

Step-by-Step 6-2: Processing Arrays

Demo

Page 17: 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

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

Page 18: 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

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()

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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” }

Page 22: 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

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

Page 23: 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

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, …)

Page 24: 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

6-24

MsgBox():Constants and Values Returned

Page 25: 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

6-25

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

Demo

Page 26: 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

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

Page 27: 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

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)

Page 28: 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

6-28

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

Demo

Page 29: 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

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 “,”

Page 30: 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

6-30

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

Demo

Page 31: 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

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

Page 32: 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

6-32

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

Demo

Page 33: 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

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

Page 34: 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

6-34

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

Demo

Page 35: 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

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