How to use Arrays in VB.NET

In this article You will learn How to use Arrays in VB.NET.
  • 1755

In this article You will learn How to use Arrays in VB.NET.

ARRAYS: Array is the collection of similar type of element. Arrays are declared using Dim, ReDim, Static, Private, Public and Protected keywords. In VB.NET Array have not fix sized.

Types of Array: there are following types of an array in VB.NET.

Single Dimensional Array:  Define the single Dimensional array as following:

Dim num As Integer() = New Integer(4) {}
num(0) = 67
num(1) = 55
Multi-Dimensional array:  Such can be of two types   
  1.   Rectangular Array
  2. Jagged Array or Complex Array

Rectangular Array:-  Having equal number of elements in each row.

 

Rectangle array is define as following:

Dim num As Integer(,) = New Integer(1, 2) {}num(0, 0) = 89

Jagged Array :

  • Array within array is knows as Jagged Array

  • Can have un-equal number of elements in each row

Define such as following:

 

Dim num As Integer()() = New num(2)() {}
num(0) = New Integer(4) {}
num(1) = New Integer(6) {}
num(2) = New Integer(8) {}

 

For example :

 

Module Module1

   Sub Main(ByVal args As String())

        Dim num As Integer() = New Integer(4) {}

        Console.WriteLine("Enter {0} number : ", num.Length)

        For i As Integer = 0 To num.Length - 1

            num(i) = Integer.Parse(Console.ReadLine())

        Next

        Console.WriteLine(" sorted array is:")

        Array.Sort(num)

        For Each n As Integer In num

 

            Console.WriteLine(n)

        Next

 

    End Sub

 

End Module

 

 

OUTPUT:

 

  array.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.