Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp

For Each Next

Vorig Onderwerp

Array Initializers

|

Array Datatype

Volgend Onderwerp
-


A For Each ... Next iteration can be used to perform an operation for each element of a collection, for instance for each element of an array.

Syntax :


 For Each <element> In <collection>
    <body-iteration>
 Next

The body of the iteration is executer for every element of the collection.
The element variable must be declared of the type of the elements of the collection. Following example iterates over Integer arrays, so the element variable needs to be declared of type Integer (1).

The In clause is used to indicate the collection of which the elements are approached. Keyword In is followed by the identifier of the collection.


Module Example1
    Sub Main()
        Dim element As Integer                                             ' (1)
        '
        Dim row() As Integer = {1, 2, 3, 4, 5, 6}
        For Each element In row
            Console.Write(element & " ")
        Next
        Console.WriteLine()
        '
        Dim matrix(,) As Integer = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}
        For Each element In matrix
            Console.Write(element & " ")
        Next
        Console.WriteLine()
        '
        Dim cube(,,) As Integer = {{{1, 2, 3}, {4, 5, 6}}, _
                                   {{7, 8, 9}, {10, 11, 12}}, _
                                   {{13, 14, 15}, {16, 17, 18}}, _
                                   {{19, 20, 21}, {22, 23, 24}}}
        For Each element In cube
            Console.Write(element & " ")
        Next
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1 2 3 4 5 6
 1 2 3 4 5 6 7 8 9 10 11 12
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

The elements of the collection are approach in the same order as they are stored in memory.

A For Each ... Next is only useful to read out the elements of the collection.


Module Example2
    Sub Main()
        Dim numbers As Integer() = {1, 2, 3, 4, 5}
        '
        Dim element As Integer
        For Each element In numbers
            Console.Write(element & " ")
        Next
        Console.WriteLine()
        '
        Dim sum As Integer
        For Each element In numbers
            sum += element
            element = 10                                                   ' (1)
        Next
        Console.WriteLine("sum : " & sum)
        '
        For Each element In numbers
            Console.Write(element & " ")
        Next
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1 2 3 4 5
 sum : 15
 1 2 3 4 5

Line (1) is useless and will not affect the elements of the array. For every execution of the body, the element variable is assigned a copy of the next element. When the element variable is changed, only the copy is affected, the original is untouched.


Updated On : 2008-10-25

Download Broncode

Published On : 2008-11-06

For Each Next

Vorig Onderwerp

Array Initializers

|

Array Datatype

Volgend Onderwerp

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.