ARTICLE

How to use Arrays in VB.NET

Posted by Rohatash Kumar Articles | Strings, Arrays in VB.NET January 27, 2011
In this article we will learn how to use Arrays in VB.NET.
 
Reader Level:

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

ARRAY

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 or An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Dimentions

The dimensionality of an array refers to the number of subscripts used to identifyan individual element.

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. Rectangular arrays are arrays in which each member of each dimension is extended in each other dimension by the same length.

 

Rectangle array is define as following:

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

num(0, 0) = 89

Jagged Array : Jagged Array is an array of arrays in whichthe length of each array can differ.

  • 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(8) {}

        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:


a1.gif 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor