Arrays in Visual Basic .NET

In this article, I will explain you about Arrays in Visual Basic .NET.
  • 4878


When we want to store similar types of values or objects, for this we generally use Arrays. They are programming constructs that store data and by numeric index or subscript they allow us to access that data. Arrays have an upper bound the starting index of the array and a lower bound the ending index of the array. In Visual Basic .NET all arrays are start from zero, which means thatthe index of the first element in the array is zero. Arrays inherit from the Array class in the System namespace. We can declare arrays by using Dim, ReDim, Static, Private, Public and Protected keywords. We must specify the number of elements in the array. One dimension arrays are linear arrays and more than one dimension arrays are multidimensional arrays. In Visual Basic .NET we can have up to 32 dimensions and their size are is not fixed. The dimensionality of an array is used to identify an individual element.


Imports System.Console
Module Module1
    Sub Main()
        Dim Student(5) As String
        'Declaring an Array
        Student(0) = "Vinne"
        Student(1) = "Simran"
        Student(2) = "Atul"
        Student(3) = "Bhavna"
        Student(4) = "Pooja"
        Student(5) = "Ankur"
        'storing values in the array
        WriteLine("Name of the Student in the fourth location" & " " & Student(3))
        'displaying value from array
    End Sub



Reinitializing Arrays

Sometime we have a situation in which we have to change the size of an array after creating them. For this we use ReDim statement which assigns a completely new array object to the specified array variable. To change the number of elements in an array you can use ReDimstatement. We should use the Preserve keyword to preserve the existing data when reinitializing an array, because when we use ReDim statement all the data contained in the array is lost. The following code show you how to change the size of the declared arrays:   


Imports System.Console
Module Module1
    Sub Main()
        Dim Student(5) As String
        ReDim Preserve Student(10)
        'Reinitializing the Array 
    End Sub
End Module

Multidimensional Arrays

In Visual Basic .NET multidimensional arrays are of two types Rectangular arrays and Jagged arrays.

  • Rectangular arrays: Rectangular arrays are those arrays in which each row contains an equal number of columns. By specifying additional dimensions at declaration we can declare a rectangular array. The following lines of code show you the declaration of a multidimensional rectangular array:

                Dim RectangularArray(4, 2) As Integer
                'declares an array of 5 by 3 members which is a 15 member array
                Dim RectangularArray(,) As Integer = {{1, 2, 3}, {12, 13, 14}, {11, 10, 9}}
        'setting initial values

  • Jagged Arrays: It is the array of arrays in which the length of every array can be differ from each other. We can use jagged array where we want to create a table in which the number of columns differ for every row. The following lines of code show you the declaration of a multidimensional jagged array:

            Dim Student(2)() As String
            'declaring an array of 3 arrays
            Student(0) = New String() {"Atul","Pooja""Bhavna"}
            'initializing the first array to 3 members and setting values
            Student(1) = New String() {"Atul","Ankur""vinne","Nilesh"}
            'initializing the second array to 4 members and setting values
            Student(2) = New String() {"Atul","Bhavna""Pooja","Ankur""Anu"}
    'initializing the third array to 5 members and setting values


I hope this article help you to understand about Arrays in Visual Basic .NET


More Articles

© 2019 DotNetHeaven. All rights reserved.