Microsoft Visual Basic 2005 BASICS Lesson 12 Arrays
Objectives Declare an array. Enter information into an array. Access the information in an array. Use loops with arrays. Use parallel arrays.
Declaring and Accessing Arrays Arrays Allow you to gather and manipulate different pieces of information of the same type
Declaring a Variable Declaring variables to manage a set of values This approach works, but is not very convenient or flexible.
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.
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.
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
Entering and Accessing the Elements in an Array (cont.)
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
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
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.
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.
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.
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.
Summary (cont.) Parallel arrays are any number of arrays, which may be of different data types, that hold information in corresponding elements.