Now it’s time to look at manipulating arrays. We will see how to append, insert, remove array elements.

To append to an array (add an element at the end of the array) you use the following code:
nameArray.append “Neville”

To insert an element into an array at a particular position you use this code:
nameArray.insert 3, “Neville” 
//all elements, including element with index number 3 are moved down and the new element, “Neville”, becomes the element with index number 3.

To remove an element from an array use the following:
nameArray.remove 3 
//remove the element with the index number 3

Now, with this appending, inserting, and removing going on it can get difficult to remember how many elements are in an array at any given moment. Fortunately there is a simple way to find out. By using the “Ubound” function we can determine the size of an array like this

Dim i as integer

i = Ubound(nameArray)

Ubound stands for the “upper bound” or the upper limit of an array. In our nameArray we have 6 elements.

nameArray(0) = Harry
nameArray(1) = Ron
nameArray(2) = Hermione
nameArray(3) = Draco
nameArray(4) = Crabbe
nameArray(5) = Goyle

Because the first element’s index number is 0 the last element’s index number is 5; not 6. Therefore, if we use “i = Ubound(nameArray)” to get the size of the array “i” will be equal to 5. The Ubound function returns the index number of the last element; not the actual number of elements.

Arrays can also be sorted very easily. To put the elements in the nameArray array in alphabetical (ascending) order we would use the code “nameArray.sort”. The result would be:

nameArray(0) = Crabbe
nameArray(1) = Draco
nameArray(2) = Goyle
nameArray(3) = Harry
nameArray(4) = Hermione
nameArray(5) = Ron

So, as you can see, arrays are not something to be avoided. They are really quite straight forward once you get to know them. Just remember they are zero based (the first element’s index is always 0), just like listbox rows, and they won’t seem so mysterious.

ArrayDemo.zip