Visual Basic Programming

Page 24

 As we approach the end of the basics in this tutorial, I hope that you have a good grasp on most of the techniques introduced to you. Hopefully, you have made a few simple applications on your own. Now I will introduce another fundamental of programming. This page will cover arrays. An array is a collection of related data stored in a variable with the same name. The variables are indexed just like objects on a form that have the same name. An array is declared with a Dim or Public statement.

Example 1: Example 2:
Dim strCapitals(49) as String Dim strCatalog(9, 2) as String

Example 1 creates an array to store text (declared as a String), consisting of 50 different parts. The number in the parenthesis specifies this number. While the number in example 1 is 49, this includes 0, just as all arrays do, therefore giving the array a total of 50 dimensions.
Example 2 creates an array to store text (declared as a String), consisting of 10 different parts. The 2 specifies a new dimension in the array, consisting of 3 different parts.

Try to picture this. Dim strArray(2, 4) as String would look like:

(0, 0) (0, 1) (0, 2) (0, 3) (0, 4)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4)

OR:

(0, 0) (1, 0) (2, 0)
(0, 1) (1, 1) (2, 1)
(0, 2) (1, 2) (2, 2)
(0, 3) (1, 3) (2, 3)
(0, 4) (1, 4) (2, 4)

It sometimes helps to picture an array as a table. An array is set up just like a table. Either way we look at this array, we still have fifteen elements (count the cells). Let us put this to use now.

Line 1 declares variable bytState as a Byte; the second line declares the variable strStates as an array, containing 10 elements.

The first line simply sets the variable bytState to zero so that it has a base to increment from. Lines 2 through 11 store a state name in an array element.

Line 1: Increment the variable bytState by one.
Line 2: Test the value of bytState. Set bytState to 0 when it becomes 10. This variable is used to access the array elements we have. This value stands in for the array element for our array of Line 3. Since we have no element 10, we will set it back to zero so that we may keep on going through the states.
Line 3: Set the caption of the form to the text in strStates, looking at whatever element (0 through 9) in the array that bytState is set to.

Test the application. As you see, it works just like an index. Now we are going to learn how to loop through an array with a For Next loop.

Line 1: Begin For Next loop, using the range of values zero to nine for bytState.
Line 2: Add the item in the array element strStates(bytState) to the list box. bytState starts at zero, so it first adds element strStates(0) to the list box. Line 3 increments it up to nine, thus adding the next element in the array (strStates(1)) to the list box, and go up to a value of 9 for bytState, thus adding everything in the array to the list box.
Line 3: Increment the loop; go to the next value of bytState up to nine.

Run the application and confirm that it works. It should add all the items in the array to the list box.

I hope you understand how to use arrays now. Proceed to Page 25 to use another array a little differently.

How do you like this tutorial? Are you learning this language easily? Please let me know via e-mail at webtechjava@yahoo.com.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30