Array Redefined in VB.NET

Definition

An array is a collection of similar type of data element or data type. Elements of an array occupies contagious location and have equally spaced addresses in the memory. Arrays in .NET enabled languages are inherited from the System.Array class. Arrays can be declared using Dim, ReDim, Static, Private, Public and Protected keywords. In VB.NET Array don't have fix sized.

Syntax/Declaration:-

Dim arrayname(size or No. of elements) As type    'Declare and initialize array of size n

OR

Dim arrayname() As type = New type(size){}

OR

Dim arrayname() As type
arrayname=New type(size){}

Example:-

Dim num(5) As Integer             'Declaration & Initialization of an array of integers of size 5

Dim num() As Integer               'Another way to declare and initialize arrays
num = New Integer() {0,1,2,3,4}  
OR

num = New Integer(5){}

Accessing Elements of Array:-

We can access elements of an array by their numerical index value. Index of the first element of array always starts with zero(0). You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the number that specifies the index of the last element in the array.

Example:-

Imports System.Console

Module Module1

   Sub main()

       Dim num(5) As Integer      'Declare and initialize array of size 5

            num(0) = 1            'First element of the array always start with index 0
       num(1) = 2
       num(2) = 3
       num(3) = 4
       num(4) = 5
      WriteLine("Element of the array at the fourth location is :- " & " " & num(3))

       Read()
 
End Sub
End Module

Output:-

access-element-from-array-in-vb.net.gif
 

Types of Array :

Arrays are of two types: - Single Dimensional Array and Multidimensional Array. Dimension of an array refers to the direction in which we can vary with the specification of the array's element. We can determine the dimensions of an array by the number of subscripts that are used to identify the position of any array element.  We must declare the dimension of the array before using them into our program.
A single dimensional array is identified by only a single subscript and  a two-dimensional array is identified by two subscripts. The declaration of an array comprises the name of the array and the number of elements the array can contain.
For example:-
Syntax to declare a single dimensional array is:-
  
Dim arrayname(size or no of elements) As type

Example:

 
Dim OneDimensionalArr(5) As Integer  'Declaration of a single dimensional array of integer type

and syntax to declare a multidimensional array is :-
  
Dim arrayname ( no. of 1st element, no. of 2nd element,.......) As type

Example:

Dim TwoDimensionalArr(1,2) As Integer    'Declaration of two dimensional array having two rows and three columns

Note: An array can have more than two dimension. An array can have up to 32 dimensions although more than three dimensions are unusual and difficult to handle and visualize. Adding a new dimension in the array will increase the storage space significantly, thus always use multidimensional arrays carefully.

Reinitilization of Arrays and Dynamic Array:

We can resize an array at run time. This can be done using ReDim statement. The ReDim statement sizes or resizes and reinitializes the elements of an array which has already been declared. The ReDim statement updates the initial declaration, and releases an existing array and create a n array with same name and type.

Example:

Imports System.Console
Module Module1

 Sub main()
   
Dim num(5) As Integer       'Declare and initialize array of size 5
        num(0) = 1                  'First element of the array always start with index 0
        num(1) = 2
        num(2) = 3
        num(3) = 4
        num(4) = 5                  'Last element of the array
       
ReDim num(7)
        WriteLine("Element of the array at the fourth location is :- " & " " & num(3))
        Read()
   
End Sub
End Module

Output:

array-types-in-vb.net.gif

We can also change the dimension of an array using ReDim keyword.

    Example

    
Dim num(1,2)As Integer     'Declaring multidimensional array
     ReDim num(2,3)                'Resizing dimension of the array

Note: ReDim is used to change the number of dimension of an array but it can't be used to make a multi dimensional array to a single dimension, or lesser than its original dimension.

ReDim statement can be used to redeclare an array.

    Example:

    
Dim num() As Integer
     ReDim num(10)

We can prevent the array of being reinitialized by using 'Preserve' keyword. Using Preserve keyword with ReDim statement, will hold the previous values of the array while sizing or resizing it.

Example:

Imports System.Console

Module Module1

    Sub main()
   
Dim num(5) As Integer          'Declare and initialize array of size 5
        num(0) = 1                     'First element of the array always start with index 0
        num(1) = 2
        num(2) = 3
        num(3) = 4
        num(4) = 5                
    'Last element of the array
       
ReDim Preserve num(7)
        WriteLine("Element of the array at the fourth location is :- " & " " & num(3))
        Read()
    End Sub
End Module

Output:

array-output-in-vb.net.gif

Note: If the array is of two or more dimensions you can only change the  size of last dimension by using Preserve keyword.

Ex.

Dim num(2,3)As Integer
    ReDim Preserve num(3,4)      'This statement will show an error because we are trying to change first dimension
    ReDim Preserve num(2,4)      'We can only change the last the dimension of the array
Dim num(2,3,4)As Integer
   
ReDim Preserve num(3,4,5)   'Will also raise an error because we are trying to change first and second dimension
   
ReDim Preserve num(2,3,5)   'We can make changes in the last dimension of the array only


Similar Articles