We worked along with loops last time. Lets get into the actual use of looping today. Though they are used for a variety of reasons, the main application of looping structures is the access of arrays.
A collection of similar variables, in which each has the same name and all are of the same type, is an array. Remember that a variable can be thought of as a cup that holds an unknown value or an always changing value
Think of an array, then, as a collection of cups. Each cup in the collection can hold the same type of data, and every cup in the collection has the same name. Each cup within the collection is an element and has a number assigned to it that reflects its position within the collection. The first element of an array usually has a position number of 0 (zero).
Arrays can be different sizes. One array might have three elements, another might have 30 elements, and it's even possible for an array to have no elements at all--only the possibility of having elements that are created at a later time.
You can declare, or dimension, an array in two ways: as you would a single variable and by using the To keyword.
To declare an array as you would a single variable, you use the following syntax:
Dim|Public|Private ArrayName(Subscript) As DataType
In this syntax,
Dim newarray(4) As Integer
To assign a value to each element in the array newarray, you would use the following:
newarray(0) = 90
newarray(1) = 34
newarray(2) = 27
newarray(3) = 10
newarray(4) = 89
To change the value of the fourth element of the array newarray from 10 to 45, you would do as follows:
newarray(3) = 45
In the next article, we shall study manipulation of arrays and
Go To Page: 1 2
| Here's the follow-up discussion on this article: | View all related messages |
For a complete listing of article comments, questions, and other discussions related to Swapna Kamat's Learning Visual Basic topic, please visit the Discussions page.