Arrays in VB.net – Types of Arrays in VB.net

What is Arrays in VB.net?

The Arrays in VB.net is a linear data structure that is a collection of data elements of the same type stored on a contiguous memory location.

Each data item is called an element of the array. It is a fixed size of sequentially arranged elements in computer memory with the first element being at index 0 and the last element at index n – 1, where n represents the total number of elements in the array.

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

The following is an illustrated representation of similar data type elements defined in the VB.NET array data structure.

VB.net Array Data Structure
VB.net Array Data Structure

Declaring Arrays in VB.net

Declaring Arrays in VB.net by specifying the data of the elements followed by parentheses () in the VB.NET

'Declaring an Arrays'

Dim array_name As [Data_Type] ()  

In the above declaration, array_name is the name of an array, and the Data_Type represents the type of element (Integer, char, String, Decimal) that will to store contiguous data elements in the VB.NET array.

Example of Declaring Arrays in VB.net:

Now, let us see the example to declare an array in VB.net.

'Store only Integer values  
Dim num As Integer() or Dim num(5) As Integer  
'Store only String values  
Dim name As String() or Dim name(5) As String  
' Store only Double values  
Dim marks As Double()  

Types of Arrays in VB.net

There are two Types of Arrays in VB.net namely:

1. Fixed Size Array in VB.net

A Fixed Size Array In VB.net is used to store a set number of elements in memory. This indicates that the array’s size cannot be altered because the number of elements we specified in the array declaration will not change during the defining of the elements.

For example, we need to hold only 5 names in an array; it can be defined and initialized in the array such as:

Dim names( 0 to 4) As String  
names(0) = "Adones"  
names(1) = "Glenn"  
names(2) = "Jaymar"  
names(3) = "Paul"  
names(4) = "Jude"  

The above representation of the fixed array is that we have defined a string array names 0 to 4, which stores all the elements in the array from 0 to index 4.

2. Dynamic Arrays In VB.net

A Dynamic Arrays In VB.net are those that can be resized and dimensioned in accordance with the requirements of the software. Using the ReDim statement, a dynamic array can be declared.

Syntax for ReDim statement:

'syntax for ReDim statement'

ReDim [Preserve] arrayname(subscripts)

Example program of Dynamic Arrays in VB.net:

Module arrayApl
   Sub Main()
      Dim marks() As Integer
      Dim i As Integer
      
      ReDim marks(2)
      marks(0) = 85
      marks(1) = 75
      marks(2) = 90
     
      ReDim Preserve marks(10)
      marks(3) = 80
      marks(4) = 76
      marks(5) = 92
      marks(6) = 99
      marks(7) = 79
      marks(8) = 75
      
      For i = 0 To 10
         Console.WriteLine(i & vbTab & marks(i))
      Next i
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result:

0 85
1 75
2 90
3 80
4 76
5 92
6 99
7 79
8 75
9 0
10 0

Test your Code Here!

Jagged Array in VB.net

A Jagged Array in VB.net is an array whose components are arrays of various shapes and sizes. A jagged array can store arrays instead of a specific data type value and is occasionally referred to as a “array of arrays“.

The following code shows declaring a Jagged Array in VB.net named scores of Integers:

'Declaring a Jagged Array'

Dim scores As Integer()() = New Integer(5)(){}

Example Program of Jagged Array in VB.net

The following example illustrates using a jagged array:

Module arrayApl
   Sub Main()
      'a jagged array of 5 array of integers
      Dim a As Integer()() = New Integer(4)() {}
      a(0) = New Integer() {0, 0}
      a(1) = New Integer() {1, 2}
      a(2) = New Integer() {2, 4}
      a(3) = New Integer() {3, 6}
      a(4) = New Integer() {4, 8}
      Dim i, j As Integer
      ' output each array element's value 
      
      For i = 0 To 4
         For j = 0 To 1
            Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i)(j))
         Next j
      Next i
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result:

a[0,0] = 0
a[0,1] = 0
a[1,0] = 1
a[1,1] = 2
a[2,0] = 2
a[2,1] = 4
a[3,0] = 3
a[3,1] = 6
a[4,0] = 4
a[4,1] = 8

Test your Code Here!

Multidimensional Arrays in VB.net

A Multidimensional Arrays in VB.net can be used to store multiple dimensions in a tabular format, such as rows and columns. The multidimensional array in VB.net supports two or three dimensions.

Declaration of Multidimensional Arrays in VB.net

Declaration of two-dimensional array  
Dim twoDimenArray As Integer( , ) = New Integer(3, 2) {}  
Or Dim arr(5, 3) As Integer  
Representation of Three Dimensional array  
Dim arrThree(2, 4, 3) As Integer  
Or Dim arr1 As Integer( , , ) = New Integer(5, 5, 5) { }  

Example Program of Multidimensional Arrays in VB.net:

Imports System  
Module MultidimenArray  
    Sub Main()  
        ' Definition of 2 Dimensional Array  
        Dim intArray(,) As Integer = {{5, 4}, {3, 2}, {4, 7}, {4, 5}}  
  
        ' Definition of 3 Dimensional Array  
        Dim threeDimen(,,) As Integer =  
            {{{1, 3, 2}, {2, 3, 4}},  
            {{5, 3, 6}, {3, 4, 5}},  
        {{1, 2, 2}, {5, 2, 3}}}  
  
        Console.WriteLine(" Two Dimensional Arraye in VB.NET are")  
        For i As Integer = 0 To 3  
            For j As Integer = 0 To 1  
                Console.WriteLine("intArray[{0}, {1}] = {2}", i, j, intArray(i, j))  
            Next j  
        Next i  
  
        Console.WriteLine(" Three Dimensional Arraye in VB.NET are")  
        For i As Integer = 0 To 2 - 1  
            For j As Integer = 0 To 2 - 1  
                For k As Integer = 0 To 4  
                    Console.WriteLine("intArray[{0}, {1}, {2}] = {3}", i, j, k, threeDimen(i, j, k))  
                Next k  
            Next j  
        Next i  
    End Sub  
End Module

When the above code is compiled and executed, it produces the following result:

Two Dimensional Arraye in VB.NET are
intArray[0, 0] = 5
intArray[0, 1] = 4
intArray[1, 0] = 3
intArray[1, 1] = 2
intArray[2, 0] = 4
intArray[2, 1] = 7
intArray[3, 0] = 4
intArray[3, 1] = 5
Three Dimensional Arraye in VB.NET are
intArray[0, 0, 0] = 1
intArray[0, 0, 1] = 3
intArray[0, 0, 2] = 2
intArray[0, 1, 0] = 2
intArray[0, 1, 1] = 3
intArray[0, 1, 2] = 4
intArray[1, 0, 0] = 5
intArray[1, 0, 1] = 3
intArray[1, 0, 2] = 6
intArray[1, 1, 0] = 3
intArray[1, 1, 1] = 4
intArray[1, 1, 2] = 5

Test your Code Here!

Array Class in VB.net

The Array class in VB.net is the base class for all the arrays in VB.net. It is defined in the System namespace. The Array class provides various properties and methods to work with arrays.

Properties of the Array Class

The following table provides some of the most commonly used properties of the Array class.

#Property Name & Description
1.IsFixedSize
Gets a value indicating whether the Array has a fixed size.
2.IsReadOnly
Gets a value indicating whether the Array is read-only.
3.Length
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
4.LongLength
Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
5.Rank
Gets the rank (number of dimensions) of the Array.
Properties of the Array Class

Methods of the Array Class

The following table provides some of the most commonly used methods of the Array class.

#Method Name & Description
1.Public Shared Sub Clear (array As Array, index As Integer, length As Integer)
Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
2.Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)
Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
3.Public Sub CopyTo (array As Array, index As Integer)
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
4.Public Function GetLength (dimension As Integer) As Integer
Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
5.Public Function GetLongLength (dimension As Integer) As Long
Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
6.Public Function GetLowerBound (dimension As Integer) As Integer
Gets the lower bound of the specified dimension in the Array.
7.Public Function GetType As Type
Gets the Type of the current instance (Inherited from Object).
8.Public Function GetUpperBound (dimension As Integer) As Integer
Gets the upper bound of the specified dimension in the Array.
9.Public Function GetValue (index As Integer) As Object
Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
10.Public Shared Function IndexOf (array As Array,value As Object) As Integer
Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
11.Public Shared Sub Reverse (array As Array)
Reverses the sequence of the elements in the entire one-dimensional Array.
12.Public Sub SetValue (value As Object, index As Integer)
Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
13.Public Shared Sub Sort (array As Array)
Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
14.Public Overridable Function ToString As String
Returns a string that represents the current object (Inherited from Object).

Summary

Arrays in VB.net are used for storing data elements that belong to the same data type. A dynamic array in VB.net allows the user to resize it by adding more elements to it.

You use the command ReDim for adding elements to an existing array The size of a fixed-size array in VB.net cannot be changed.


The array elements are ordered using indexes, with the first element being at index 0 and the last element at index n-1, where n is the total number of array elements. Arrays can be deleted using the Erase function You can use split and join functions to split or join a string array respectively.


Leave a Comment