Visual Basic 2008 9.0 .NET Examples and Ebook

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp

Array Initializers and For Each Next

Vorig Onderwerp

Dynamical Arrays

|

Array Datatype and Jagged Arrays

Volgend Onderwerp
Array Initializers

Array Initializers

For Each ... Next Iteration

For Each ... Next Iteration



Array Initializers


When no upperbound is used in the declaration of an array variable, the array can be initialized. This can be done by adding an initialization clause to the declaration, containing initial element values, separated by commas, and surrounded by braces ( {...} ).

The arrayinitializer is a expression that will create the array instance ( with the defined values ), and will evaluate to the reference of that array instance. That reference can then be assigned to the array variable.


Module Example1
    Sub Main()
        Dim indexFirstDimension As Integer
        Dim indexSecondDimension As Integer
        Dim indexThirdDimension As Integer
        '
        Dim row() As Integer = {1, 2, 3, 4, 5, 6}
        For indexFirstDimension = 0 To 5
            Console.Write(row(indexFirstDimension) & " ")
        Next
        Console.WriteLine()
        '
        Dim matrix(,) As Integer = {{1, 2, 3, 4, 5, 6}, _
                                    {7, 8, 9, 10, 11, 12}}                 ' (1)
        For indexFirstDimension = 0 To 1
            For indexSecondDimension = 0 To 5
                Console.Write(matrix(indexFirstDimension, _
                                     indexSecondDimension) & " ")
            Next
        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}}}                         ' (2)
        For indexFirstDimension = 0 To 3
            For indexSecondDimension = 0 To 1
                For indexThirdDimension = 0 To 2
                    Console.Write(cube(indexFirstDimension, _
                                       indexSecondDimension, _
                                       indexThirdDimension) & " ")
                Next
            Next
        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

row :


 1st index : 0 1 2 3 4 5
     value : 1 2 3 4 5 6

matrix :


 1st index : 0 0 0 0 0 0 1 1 1 1  1  1
 2nd index : 0 1 2 3 4 5 0 1 2 3  4  5
     value : 1 2 3 4 5 6 7 8 9 10 11 12

matrix :


 1st index : 0 0 0 0 0 0 1 1 1 1  1  1  2  2  2  2  2  2  3  3  3  3  3  3
 2nd index : 0 0 0 1 1 1 0 0 0 1  1  1  0  0  0  1  1  1  0  0  0  1  1  1
 3rd index : 0 1 2 0 1 2 0 1 2 0  1  2  0  1  2  0  1  2  0  1  2  0  1  2
     value : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Klik hier om terug naar boven te gaan.  Up



For Each ... Next Iteration


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 Example2
    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 Example3
    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.





This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-01-24

Download Broncode

Published On : 2008-06-24

Array Initializers and For Each Next

Vorig Onderwerp

Dynamical Arrays

|

Array Datatype and Jagged Arrays

Volgend Onderwerp

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).