For Each Next Loop

When your procedure needs to loop through all of the objects of a collection or all of the elements in an array (arrays are the subject of the next chapter), the For Each.. .Next loop should be used. This loop does not require a counter variable. Visual Basic can figure out on its own how many times the loop should execute. The For Each...Next loop looks like this:

For Each element In Group statementl statement2 statementN Next [element]

Element is a variable to which all the elements of an array or collection will be assigned. This variable has to be of the Variant data type for an array and of the Object data type for a collection. Group is a name of a collection or an array. Let's now see how to use the For Each.. .Next loop to print the names of controls in the currently open form to the Immediate window.

0 0

Post a comment