Arrays Continued | ||
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: To insert an element into an array at a particular position you use this code: To remove an element from an array use the following: 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 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 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. | ||