Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp

Array Datatype

Vorig Onderwerp

For Each Next

|

Jagged Arrays

Volgend Onderwerp
-


An array variable contains Nothing or a reference to an array instance.

When an identifier of an array variable is used as an expression ( for instance expression array1 in line (1) ) this expression will evaluate to the reference of that array instance.
Line (1) will assign the reference of the first array to the array variable array2.
Both variables ( array1 and array2 ) now point to the same array instance. As you can see lines (2) and (3) have the same result.

Lines (4) and (5) would result in a compile error, because variables of types Integer(,) ( two-dimensional Integer array ) or Short() ( one-dimensional Short array ) cant hold a reference to an instance of an array of type Integer() ( one-dimensional Integer array ).

Only array variables of type Integer() ( one-dimensional Integer array ) can hold a reference to an one-dimensional Integer array instance.
Well actually this is not completely correct, array variables of type Array ( System.Array ) can hold references to any type of arrays.
The reason for this is that 'Array' is the base type for all other arraydatatypes. All variables declared of a base type can hold references to instances of derived types. In the topics about inheritance and polymorphism we'll discuss this in detail.
So variable array6 can for instance hold a reference to a two-dimensional Byte array (6) or for instance a reference to a one-dimensional Integer array (7).


Module Example
    Sub Main()
        Dim array1() As Integer = {1, 2, 3, 4, 5}
        Dim array2() As Integer = array1                                   ' (1)
        Console.WriteLine(array1(3))
        Console.WriteLine(array2(2))
        '
        array1(3) = 33
        Console.WriteLine(array1(3))                                       ' (2)
        Console.WriteLine(array2(3))                                       ' (3)
        '
        array2(4) = 44
        Console.WriteLine(array1(4))
        Console.WriteLine(array2(4))
        '
        'Dim array3(,) As Integer = array1                                 ' (4)
        'Dim array4() As Short = array1                                    ' (5)
        '
        Dim array5(,) As Byte = {{1, 2}, {3, 4}}
        '
        Dim array6 As Array = array5                                       ' (6)
        Console.WriteLine(array6(1, 1))
        '
        array6 = array2                                                    ' (7)
        Console.WriteLine(array6(4))
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 4
 3
 33
 33
 44
 44
 4
 44

Updated On : 2008-10-25

Download Broncode

Published On : 2008-11-06

Array Datatype

Vorig Onderwerp

For Each Next

|

Jagged Arrays

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.