Top Banner
Microsoft Visual Basic 2005 BASICS Lesson 12 Arrays
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS

Lesson 12

Arrays

Page 2: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 2

Objectives

Declare an array. Enter information into an array. Access the information in an array. Use loops with arrays. Use parallel arrays.

Page 3: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 3

Declaring and Accessing Arrays

Arrays Allow you to gather and manipulate

different pieces of information of the same type

Page 4: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 4

Declaring a Variable

Declaring variables to manage a set of values This approach works, but is not very

convenient or flexible.

Page 5: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 5

Difficulties with Using Variables

Difficulties Entering values is very tedious. It is very easy to make a mistake. Solutions do not scale well.

Solution Use arrays instead.

Page 6: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 6

Declaring Arrays

An array allows you to refer to a series of variables by the same name and to use an index, or subscript, to tell them apart.

Visual Basic arrays are zero-based. The size of the array must be declared. An array can be declared with an

initialization list.

Page 7: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 7

Entering and Accessing the Elements in an Array

Index (or subscript) Identifies the element to process Can be specified using a variable or an

equation Must be a value between zero and the

upper bound

Page 8: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 8

Entering and Accessing the Elements in an Array (cont.)

Page 9: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 9

Making an Array User-Friendly

Important to trap errors with arrays Verify index values before using them

Zero-based array does not always fit Programmer can hide the way information

is stored Allow users to enter values in the way they

think of the data (i.e., not zero-based) Have the program add or subtract numbers

to correctly index into the array

Page 10: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 10

Making an Array User-Friendly (cont.)

Page 11: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 11

Array Methods and Properties

Length property Returns the number of elements in an

array GetLowerBound and GetUpperBound

methods Return integers representing the lower and

upper bounds of an array

Page 12: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 12

Using Loops with Arrays

Common ways that arrays and loops are used together A loop can be used to apply the same

process to each element in an array. A loop can be used to prompt for input for

each element in an array and set its value. An array can be searched by using an If

statement in a loop that compares each element of the array with a specific value.

Page 13: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 13

Using Loops with Arrays (cont.)

Page 14: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 14

Parallel Arrays

Parallel arrays Arrays of the same length Each element in one array is logically

related to an element in the second array with the same index value

Page 15: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 15

Parallel Arrays (cont.)

Page 16: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 16

Summary

Visual Studio 2005 supports arrays that use an index or subscript to access different variables with the same name.

All elements of an array are of the same data type.

Arrays are declared by specifying the upper bound of the array.

Page 17: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 17

Summary (cont.)

Arrays can be declared using initialization lists that set the starting values of the array. When arrays are declared using initialization lists, the upper bound is calculated by the system based on the number of items in the list.

Individual elements in an array are read or changed by using an index value in parentheses to identify the element to use.

Page 18: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 18

Summary (cont.)

The GetLowerBound and GetUpperBound methods can be used to check that an index value is in the allowable range.

The Length method of an array returns the number of elements in an array.

Arrays can be processed sequentially using a loop.

Page 19: Ppt lesson 12

Microsoft Visual Basic 2005 BASICS 19

Summary (cont.)

Parallel arrays are any number of arrays, which may be of different data types, that hold information in corresponding elements.