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.
Open a new project in Visual Basic.
Name the project Arrays.
Name the form frmArray.
Clear the form's caption.
Set the form's BorderStyle property to 1-Fixed Single and its MinButton property to True.
Set the form's StartUpPosition property to 2-Center Screen.
Place a command button on the form and name it cmdNext.
Open the code window for that command button.
Select General in the object box of the code window, then type the following lines:
Dim bytState As Byte
Dim strStates(9) as String
Line 1 declares variable bytState as a Byte; the second line declares the variable strStates as an array, containing 10 elements.
Go to the code for the form's load procedure.
Press tab, then type the following lines:
bytState = 0
strStates(0) = "Alabama"
strStates(1) = "Alaska"
strStates(2) = "Arizona"
strStates(3) = "Arkansas"
strStates(4) = "California"
strStates(5) = "Colorado"
strStates(6) = "Connecticut"
strStates(7) = "Delaware"
strStates(8) = "Florida"
strStates(9) = "Georgia"
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.
Open the code window for the command button cmdNext.
Press tab, then type the following lines:
bytState = bytState + 1
If bytState = 10 Then bytState = 0
frmArray.Caption = strStates(bytState)
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.
Delete the entire sub-procedure for the command button cmdNext.
Delete the command button cmdNext from the form.
Place a list box on the form.
Unlock the controls and make the list a bit larger.
Center it in the form.
Name the list box lstStates.
Click the drop-down arrow for the list property of the list box, then click away to clear the text in the list box.
Delete the first line in the form's Load sub-procedure --> the line that sets bytState to zero.
Press enter at the end of the sub-procedure to create a line below the array that we set up (all the strStates variables).
Enter the following lines:
For bytState = 0 To 9
lstStates.AddItem strStates(bytState)
Next bytState
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.