The For Each Next Statement

You can use a For..Next statement to access each element of an array. Dim arrSalesPerDay() As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7} Dim I As IntegerFor I = 0 To UBound(arrSalesPerDay) Debug.Write(arrSalesPerDay(I)) Next I

You also can use a For Each..Next statement to access each element of an array. A For Each...Next loop is similar to the For...Next loop. Both execute a fixed number of times. The difference is that the number of times a For..Next loop executes depends on the values of the start, end, and step expressions, while the number of times a For Each...Next loop executes depends on the number of elements in an array or collection.

The syntax of a For Each...Next statement is: For Each element In group [ statements ] [ Exit For ] [ statements ] Next [ element ]

Table 8-6 lists and describes the parts of this statement.

Table 8-6: Parts of the For Each..Next Statement

Part

Required or Optional

Description

element

Required

Variable of the data type of group. Used to iterate through the elements of the array.

group

Required

Object variable that refers to an array. May also refer to a collection.

statements

Optional

Code that executes each iteration of the loop.

Exit for

Optional

Ends the loop prematurely,

Table 8-6: Parts of the For Each..Next Statement

Part

Required or Optional

Description

before all elements of the array have been accessed.

The following code uses a For Each..Nlext statement to output the value of each element of the array to the Debug window:

Dim arrSalesPerDay() As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7} Dim intElement As Integer For Each intElement In arrSalesPerDay Debug.Write(intElement) Next Ask the Expert

Question: Which is better to use with an array, a For Each..Nlext statement or a For..Nlext statement?

Answer: Either works fine to access the entire array. I prefer the For Each..Nlext statement simply because it relieves me from having to worry about the number of elements in the array. However, if the position of an element in the array is important, a For..Nlext statement may be preferable since code in it usually has the value of the index of the current array element.

You may have a For Each..Nlext statement even if there are no elements in the group. In this situation, the statements inside the loop simply will not execute.

Once the For Each...Next statement is reached in code, the statements are executed for the first element in the group, and then for the second element (if any), and so on until all of the elements of the group have been accessed. When there are no more elements to access, the loop is terminated and execution continues with the statement following the Next statement.

The Exit For statement serves the same function with a For Each..Next statement as it does with a For..Next statement. You also can nest For Each...Next statements just as you can For.Next statements.

1-Minute Drill

■ Do you need to specify in a For Each...Next statement the number of elements in the array? No. This is what distinguishes a For Each..Next statement from a For...Next statement.

Project 8-1: Day of Week Calculator

Surprisingly often I want to know the day of the week of an upcoming birthday or other particular date. This application will enable you to choose any date from 1990 through 2000 and determine the day of the week that date falls on. Figure 8-1 shows the project in action.

¡^Day of Week Calculator

1

I February _*J

127 j-l ]2000 _*]

Calculate ]

Sunday

Figure 8-1: Day of Week Calculator

Figure 8-1: Day of Week Calculator

This project introduces the ComboBox control. This control lists choices from which the application user may pick. This project will have three ComboBox controls, from which the user may choose the month, day, and year respectively. Having the application user choose the date from the ComboBox controls eliminates the possibility of the application user choosing an invalid date. We will use arrays and loops to populate the ComboBox controls, and also use arrays to verify that t he values in the "day" ComboBox control do not exceed the maximum number of days in the chosen month.

Once the user chooses a date using the three ComboBox controls, we will use a DateTime class, and the DayOfWeek property of that class, to determine the day of the week that the particular date falls on.

The code may appear somewhat complex, particularly in leap years, when February has 29 days instead of 28. However, the code appears more complex than it really is. Just be patient and go through the code step by step. Indeed, you may find a better way to write it!

Was this article helpful?

0 0

Post a comment