Using the For…Each Statement to 'loop' through the elements of an Array


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)

Message Box

which results in the value for that element, the letter 'D' being displayed in a Message Box.

Accessing all of the items within a Static Array

One of the great benefits of an array is the speed and ease with which you access every one of its elements.

Using what I call the Brute Force method, you can display each element of our Array on the form using this code…

Dim x As Integer

For x = 0 To 6

Form1.Print strLetters(x)

Next x

Form

I call this the Brute Force method because it requires that you know the subscript value for the first value in the Array (usually, BUT NOT ALWAYS 0) and the last value in the Array. The programmer will know these values for a Static Array (an array declared with a definitive Upper Bound) but won't necessarily know these values for a Dynamic Array (one whose Upper Bound is determined at run time).

 

<< Previous | Next >>

Contents

- Create A Static Array
- Access A Static Array
- Create A Dynamic Array
- Access A Dynamic Array

 

Main VB Page
Back to Tutorials

Search For More Tutorials

Google