Visual Basic Programming

Page 27

As mentioned in the beginning of the previous page, we are now going to access array elements using a random number. We are going to set up a states and capitals program that gives the user a state and asks for the capital. The state will be stored in one dimension of the array, the correct answers (the capitals) in the next dimension, and the user's entry in the next dimension. This will becomes more clear shortly.

I hope you understand this code by now.

Most of those variables should be self-explanatory as to what they will store. The variables bytMinutes and bytSeconds are going to store the total amount of time it takes the user to fill in all the capitals. The variable strCaptials is set up as an array 0 to 49 and 0 to 2. This stores the state, the capital,  and the users guess.

Obviously, this code begins in incrementing the variable bytSeconds by 1, then tests to see If the value of bytSeconds has reached 60 (which is one minute because the Interval of the timer is set to one second). When it reaches sixty, increment the variable bytMinutes by one, then reset bytSeconds to zero.

Now we will set up the array. Remember that we only want to store the state and the capital. The user's answer is written to the array during runtime.

As you see in the array, we store the state in array dimension 0. The first number is just the state number (not the actual number that the state has in the US, but the index of that element in the array). The states are stored 0 through 49, which is fifty total elements, just as there are fifty total states in the US. The capital is stored the same way, but in array dimension 1. If this is difficult to picture see below.

The array looks like this:

State Capital User Entry
Alabama Montgomery  
Alaska Juneau  
Arizona Phoenix  
Arkansas Little Rock  
California Sacramento  

And so forth.

Line 1: Begin function called State.
Line 2: Set the variable intState equal to a random number times fifty (which includes values 0 through 49), then convert the result to an integer.
Line 3: Set the caption of the label lblState(1) to the text in the array strCapitals with the value of intState, looking in dimension zero.
Line 4: End the function.

Line 1: Initialize the random number generator.
Line 2: Generate a random number from 0 to 49 and store the number in the variable intState.
Lines 3 through 7 set the variables listed to zero to give them a base value.

Line 1: Set the caption of the label lblState(1) to the text in the string array dimension with the value of intState, looking in part 0 (dimension zero).
Line 2: Set the caption of the label lblCorrect to "Correct: " and the value of the variable bytCorrect, which is zero when the form loads.
Line 3: Set the caption of the label lblIncorrect to "Incorrect: " and the value of the variable bytIncorrect, which is zero when the form loads.

Now we are going to finish this application off by coding the cmdGuess command button. Be ready to type a whole lot more code!

Line 1: Declare constant conBtns as an Integer to store a Yes and No button, which we will use to prompt the user later.
Line 2: Declare variable intResponse as an Integer to store the user's selection in the message box.
Line 3: Declare variable bytDimension as a Byte to look in the array elements.
Line 4: Set the strCapitals array dimension 2, looking at the part with the same value of the variable intState to the text in the txtCapital text box.
Line 5: Increment the variable intTries by one.

Line 1: Test to see if the strCapitals array element 1 (the capital of the state) and array element 2 (the user's guess) are equal AND check to make sure the value of intState is less than 50.
Line 2: When all of the above is True (basically, the guess is the capital), add one to bytCorrect.
Line 3: Reset the variable bytWrongGuess.
Line 4: Set the caption of the label lblCorrect to "Correct: " (which resets the label) and the value of the variable bytCorrect.
Line 5: Call the function State to get a new random number to set in place for one of the array elements.

Line 1: Begin Do Until loop --> it will execute the loop statements (the next line) until the Length element 2 of array strCapitals looking at the dimension value of intState is equal to zero, or when there is no text in that dimension.
Line 2: Test to make sure that the value of the variable intTries is less than 50. When intTries is less than 50, call the function State, which generates the random number then looks in the array dimension according to that value (if the value is 15, it looks in array dimension 15, and so forth). It keeps calling the function until the array element corresponding with the value of intState is blank, which means the user has not yet entered text into that element.
Line 3: Make the Do Until loop loop. Tell the loop to keep looking in the array Until there is no text in that dimension (which line 1 does).

Line 1: Decrement intTries by one (subtract one).
Line 2: Add one to the variable bytIncorrect.
Line 3: Set the caption of the label lblIncorrect to "Incorrect: " and the value of bytIncorrect.
Line 4: Increment the variable bytWrongGuess by one.
Line 5: Test the value of the variable bytWrongGuess; when it is 2, execute statements on lines 6 through 9.
Line 6: Display a message box telling the user the capital of the state currently displayed in the label (the state in array element strCapitals(intState, 0). Basically, if the user has already typed in a capital wrongly, this prevents them from entering the capital in for the same state again wrongly. Then we tell them in the message box.
Line 7: Set the variable bytWrongGuess to 1.
Line 8: Subtract one from the variable bytIncorrect so that the user does not keep incrementing their wrong answers.
Line 9: Set the caption of the label lblIncorrect to "Incorrect: " (to reset it to normal) and the value of bytIncorrect.

This ends the first If statement in the sub-procedure. It was a very LONG If statement as you see.

Line 1: Set the selection start (SelStart) of the text box txtCapital to zero, thus moving the cursor all the way to the left of the text box.
Line 2: Set the selection length (SelLength) of the text box to the length (Len) of the text in txtCapital, thus selecting the entire string (the existing text).
Line 3: Exit the sub-procedure. We will soon enter code that we do not want the sub-procedure to execute (recall the Goto Finish line earlier).

Line 1: Set the label Finish for the code we entered earlier to Goto.
Line 2: Display a message box telling the user how many capitals they answered correctly (bytCorrect - bytIncorrect), how many minutes it took them, and how many seconds it took them. Then ask them if they would like to try again, and store their response in the variable intResponse. The underscore treats the line beneath it as part of its own line. Since this is a rather long line of code, it looks much better to enter code on multiple lines.
Lines 3 and 4 continue the message box.
Line 5: Test to see what button the user pressed in the message box. When they press Yes, execute lines 6 through 17.
Line 6: Look at values 0 to 49 for dimension 2 of the array strCapitals by way of the variable bytDimension.
Line 7: Clear elements 0 through 49 (values from line 1) of dimension 2 in the array strCapitals, thus clearing all of the user's answers.
Lines 9 through 13 reset the listed variables to zero (their initial state).
Line 14: Set the caption of the label lblCorrect to Correct: (which resets the label) and the value of bytCorrect (now set to zero).
Line 15: Same as line 14, but with the label lblIncorrect and the variable bytIncorrect.
Line 16: Call the function State in order to find another state at random.
Line 18: Otherwise (user does not want to try again), unload the form.

Well done getting through this tutorial. I hope that you made it through error-free!

Now you may proceed to Page 28.

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