Warning: include(/../inc/header.php): failed to open stream: No such file or directory in /Users/anthonycrowther/Sites/cssouth.uk/pages/array.php on line 2

Warning: include(): Failed opening '/../inc/header.php' for inclusion (include_path='.:/usr/local/Cellar/php/7.2.8/share/php/pear') in /Users/anthonycrowther/Sites/cssouth.uk/pages/array.php on line 2

Array

An array (list in Python)is a data structure that contains a set of values that are all of the same data type.

An array allows you to refer to these related values by the same name and to use a number, called an index (or subscript), to tell them apart. The individual values are called the elements of the array. They are contiguous from index 0 through to the highest index value.

You can declare an Array in VB in exactly the same way as you would any other variable:


Dim examPercentages(10) As Integer

This code declares an array of integers called examPercentages that contains 10 elements (values).

We can populate the elements of an array when we declare it...


Dim examPercentages As Integer() = New Integer(10) {45,76,84,79,68,93,87,65,88,67}

Or we can declare it first and then populate the elements afterwards...


Dim examPercentages(10) As Integer
examPercentages(0) = 45 
examPercentages(1) = 67 
examPercentages(2) = 76 
... ... 
examPercentages(9) = 84

Visually, an array can be represented as a table:

index
0
1
2
3
4
5
6
7
8
9
value
45
76
84
79
68
93
87
65
88
67

Notice how the array index starts at zero! This means that the last element in the array is element number 9 (element number 10 doesn't exist!).

To access an element in an array, we must specify its index (position in the array). For example:


Dim myExamResult As Integer myExamResult = examPercentages(5) Debug.Print(myExamResult)

In the example code above, we are accessing the 6th element (index #5) of the array examPercentages and assigning it to the integer variable myExamResult before printing it to the screen.

If you try to access an element that doesn't exist [e.g. examPercentages(10)], or one that hasn't yet been assigned a value, you will get an IndexArrayOutOfBoundsException error & the compiler will throw a wobbly and fall over!


Warning: include(/../inc/footer.php): failed to open stream: No such file or directory in /Users/anthonycrowther/Sites/cssouth.uk/pages/array.php on line 79

Warning: include(): Failed opening '/../inc/footer.php' for inclusion (include_path='.:/usr/local/Cellar/php/7.2.8/share/php/pear') in /Users/anthonycrowther/Sites/cssouth.uk/pages/array.php on line 79