|
Let's create and initialize a Static Array
NOTE: I found some software that makes development a lot faster and improves windows in general:
Before I show you how to loop through the elements of an array, let's first declare an array and then load it with values that represent the letters 'A' through 'G'. Assigning values to an element of an array simply requires that we reference the element with a subscript value contained within parentheses. Here's the code to do that…
Dim strLetters(6) As String
strLetters(0) = "A"
strLetters(1) = "B"
strLetters(2) = "C"
strLetters(3) = "D"
strLetters(4) = "E"
strLetters(5) = "F"
strLetters(6) = "G"
For those of you not familiar with the syntax, the declaration
Dim strLetters(6) As String
tells Visual Basic that we wish to allocate a String Array called strLetters whose Upper Bound is 6 (I analogize this, in my teaching and writing, to the top floor of a high rise building) What is unstated here is the Lower Bound value, which by default is 0 (I analogize this to the lower floor of a high rise building.) You could also write the declaration in this way…
Dim strLetters(0 to 6) As String
to make the code more readable, but in practice, most programmers don't specify the lower bound (which can be any value at all provided it is less than the upper bound.)
To summarize, what we have here is a one-dimensional array called strLetters containing seven elements, with elements numbered from 0 to 6.
Accessing individual items within the Array
Accessing individual items within an Array is pretty easy---we just refer to the element by using its subscript value within parentheses. For instance, to display the value of the fourth element of our array (subscript number 3) in a message box, we execute this code…
MsgBox strLetters(3)
which results in the value for that element, the letter 'D' being displayed in a Message Box.

Next >> |