Before using an array, declare the array.
Syntax:
Dim array name ([expression 1 [, expression 2…]) [As data type]
You can declare one-dimensional arrays and two-dimensional arrays based on the number of expressions given. The data type is the data type of array elements. The expression is a constant. For example:
Dim a1 (10) As Integer 'declares a one-dimensional array with subscripts ranging from 0 to 10, with a total of 11 elements.
Dim a2 () As string' declares a two-dimensional array of 6 rows and 5 columns, with 30 elements in total.
You can initialize an array while declaring it. For example:
Dim arr (4) As Integer = {10, 20, 30, 40, 50}
The upper right side is called the initialization table. When the element values of the array are given in the initialization table, the size Declaration of the array in the Dim definition statement can be omitted. The preceding array declaration statement is equivalent:
Dim arr () As Integer = {10, 20, 30, 40, 50}
The reference format for elements in the array is: array name (subscript 1, subscript 2 ,......). For example:
A1 (0) = 5' assign 5 to the first element of the array
I = a1 (3) 'assigns the 4th elements of the array to I
Note: When referencing an array element, the subscript cannot exceed the declared range. For example, for Array Dim arr (4), the subscript of available array elements is 0 ~ 4.
The following code uses the For loop to set each element in the integer array to its index value.
Dim arr (5) As Integer
Dim I As Integer
For I = 0 To 5
Arr (I) = I
Next
Example 3.6: display the average table 3-14 and obtain the average score. A total of 9 students scored 90, 89, 76, 73, 100, 98, 65, 54, and 85 respectively.
|
| Figure 3-14 shows the average score table and average score |
Source code is as follows Ex3-6.aspx ):
| BibliographyPrevious sectionNext section |