Declare An Array

You can declare an array a group of the same type of data values. You declare an array in the same fashion as you declare any other variable. Just like other variables, you declare arrays as either local or global variable arrays. You specify the scope of an array with either the Dim, Private, or Public statements. See the section "Using Global Variables" for more information about setting the scope of a variable.

You declare arrays to store a group of related data. The array stores data with the same data type;for example, integers, strings, and so on. Use of arrays greatly simplifies your code because you only declare one variable to store several values. For example, you can declare an array to store a list of students in a class. Instead of creating a separate variable for each student, such as student1, student2, and so on, you can create one array that contains all student names.

When declaring an array, you can also specify its size, where size is the number of elements in the array, as in the example: Dim Students(i To 50) As String.An element is an individual data value in the array, such as a student name. You specify the size of the array by placing the value in parentheses after the name of the array. In the example, the Students array has a size of 50 elements with the lower bound of the array of 1 and the upper bound of the array of 50.

You refer to each value you add to an array as an array element. You access elements of the array with an index value, which represents the desired element of the array. To access the second element of the Students array, the index value is 2, as in Students(2), for example.

DECLARE AN ARRAY

DECLARE AN ARRAY

□ Create a new subroutine.

L0 Initialize the values for the array elements.

■ Each array element is specified by an index value in parentheses.

□ Create a new subroutine.

Type Dim NewArray(1 To N) As DataType, replacing NewArray with the name of the new array, N with the upper bound of the array, and DataType with the data type of the array elements.

Note: For more information on Data Types see Chapter 3.

L0 Initialize the values for the array elements.

0 0

Post a comment