Arrays and Looping Statements

Several looping statements that you learned in Chapter 6 (see the For.. .Next loop and For Each.. .Next loop) will come in handy now that you're ready to perform tasks such as populating an array or displaying the elements of an array. It's time to combine the skills you've learned so far. How can you rewrite the FavoriteCities procedure so that it shows the name of each city in a separate message box?

The FavoriteCities2 procedure shown below replaces the last statement of the original procedure with the For Each.. .Next loop:

Sub FavoriteCities2()

'now declare the array Dim cities(6) As String Dim city As Variant

'assign the values to array elements cities(1) = "Baltimore"

cities(2) = "Atlanta"

citiesp) = "Boston"

cities(4) = "Washington"

cities(6) = "Trenton"

'display the list of cities in separate messages For Each city in cities MsgBox city

Next End Sub

Notice that the For Each.. .Next loop uses the variable city of Variant data type. As you recall from the previous chapter, For Each. Next allows you to loop through all of the objects in a collection or all of the elements of an array and perform the same action on each object or element. When you run the FavoriteCities2 procedure, the loop will execute as many times as there are elements in the array.

Let's take a look at yet another variation of the FavoriteCities procedure. In Chapter 4 you practiced passing arguments as variables to subroutines and functions. The procedure FavoriteCities3 demonstrates how you can pass elements of an array to another procedure. 1. In the current module, enter following the two procedures:

Sub FavoriteCities3() 'now declare the array Dim cities(6) As String

'assign the values to array elements cities(1) = "Baltimore"

cities(2) = "Atlanta"

cities(3) = "Boston"

cities(4) = "Washington"

cities(6) = "Trenton"

'call another procedure and pass the array as argument Hallo cities() End Sub

Sub Hallo (cities() As String) Dim counter As Integer For counter = 1 to 6

MsgBox "Hello " & cities(counter)


End Sub

The declaration of the Hallo procedure includes an array type argument —cities().

2. Run the FavoriteCities3 procedure. Passing array elements from a subroutine to a subroutine or function procedure allows you to reuse the same array in many procedures without unnecessary duplication of the program code.

Tip 7-5: Passing Arrays between Procedures

When an array is declared in a procedure, it is local to this procedure and unknown to other procedures. However, you can pass the local array to another procedure by using the array's name followed by an empty set of parentheses as an argument in the calling statement. For example, the statement Hallo cities() calls the procedure named Hallo and passes to it the array cities().

Here's how you can put to work your newly acquired knowledge about arrays and loops in real life. If you're an avid lotto player who is getting tired of picking your own lucky numbers, have Visual Basic do the picking. The Lotto procedure below populates an array with six numbers from 1 to 51.

Sub Lotto() Const spins = 6 Const minNum = 1 Const maxNum = 51

Dim t As Integer Dim i As Integer Dim myNumbers As String looping variable in outer loop looping variable in inner loop string to hold all picks

Dim lucky(spins) As String 'array to hold generated picks myNumbers = ""

For t = 1 To spins Randomize lucky(t) = Int((maxNum-minNum+1) * Rnd )+ minNum) 'see if this number was picked before For i = 1 To (t-1)

If lucky(t)=lucky(i) Then lucky(t) = Int((maxNum-minNum+1) * Rnd)+ minNum) i = 0 End If Next i

MsgBox "Lucky number is " & t & lucky(t) myNumbers = myNumbers & " -" & lucky(t) Next t

MsgBox "Lucky numbers are " & myNumbers

The Randomize statement initializes the random number generator. The instruction Int((maxNum-minNum+1) * Rnd + minNum) uses the Rnd function to generate a random value between the specified minNum and maxNum. The Int function converts the resulting random number to an integer. Instead of assigning constant values for minNum and maxNum, you can use the InputBox function to get these values from the user.

The inner For.. .Next loop ensures that each picked number is unique—it may not be any one of the previously picked numbers. If you omit the inner loop and run this procedure multiple times, you'll likely see some occurrences of duplicate numbers pop up.

0 0

Post a comment