ppt lesson 12
TRANSCRIPT
Microsoft Visual Basic 2005 BASICS
Lesson 12
Arrays
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.
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
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.
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.
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.
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
Microsoft Visual Basic 2005 BASICS 8
Entering and Accessing the Elements in an Array (cont.)
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
Microsoft Visual Basic 2005 BASICS 10
Making an Array User-Friendly (cont.)
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
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.
Microsoft Visual Basic 2005 BASICS 13
Using Loops with Arrays (cont.)
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
Microsoft Visual Basic 2005 BASICS 15
Parallel Arrays (cont.)
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.
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.
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.
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.