For Each...Next Statement

   
For Each...Next Statement

Syntax

 For Each   element   In   group   [   statements   ] [Exit For] [   statements   ] Next [   element   ] 
element (required; Object or any user -defined object type)

An object variable to which the current element from the group is assigned

group (required)

An object collection or array

statements (optional)

A line or lines of program code to execute within the loop

Description

Loops through the items of a collection or the elements of an array

Rules at a Glance

  • The For Each...Next code block is executed only if group contains at least one element. If group is an empty collection or an array that has not yet been dimensioned, an error (runtime errors 92, "For loop not initialized ," and 424, "Object required," respectively, or a NullReferenceException exception) results.

  • All statements are executed for each element in group in turn until either there are no more elements in group or the loop is exited prematurely using the Exit For statement. Program execution then continues with the line of code following Next .

  • For Each...Next loops can be nested, but each element must be unique. For example:

     For Each   myObj   In AnObject     For Each   subObject   In myObj         SName = subObject.NameProperty     Next Next 

    uses a nested For Each...Next loop, but two different variables , myObj and subObject , represent element .

  • Any number of Exit For statements can be placed within the For Each... Next loop to allow for premature, conditional exit of the loop. Once the loop is exited, execution of the program continues with the line immediately following the Next statement. For example, the following loop terminates once the program finds a name in the myObj collection that has fewer than ten characters :

     For Each subObject In myObj     SName = subObject.NameProperty     If Len(Sname) < 10 then        Exit For     End if Next 

Programming Tips and Gotchas

  • Because the elements of an array are assigned to element by value, element is a "local" copy of the array element and not a reference to the array element itself. This means that you cannot make changes to the array elements, as the following example demonstrates :

     Dim sArray(2) As String Dim ele As String sArray (0) = "aa" sArray (1) = "bb" For Each ele In sArray    ele = "xx"    Console.WriteLine(ele) Next For Each ele In sArray    Console.WriteLine(ele) Next 

    The output is:

     xx xx aa bb 

    which shows that the original array has not been changed.

VB.NET/VB 6 Differences

In VB 6, element had to be a variable of type Variant. VB.NET removes this restriction; element can be a strongly typed data type, as well as a variable of type Object, VB.NET's "universal" data type.

See Also

For...Next Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net