ARTICLE

Arrays in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic Language June 13, 2011
In this article I will explain about the Arrays and length property of array in Visual Basic.
 
Reader Level:

In this article I will explain about the Arrays and length property of array in Visual Basic.

Array - An array is a data structure that contains a number of variables of the same type. Arrays are declared with a type. There are different ways of declaring arrays. Arrays are declared much like variables, with a set of [] brackets after the data type, like this.

string[] names;

You need to instantiate the array to use it, which is done like this:

string[] names = new string[2];

The number (2) is the size of the array, that is, the amount of items we can put in it. Putting items into the array is pretty simple as well:

names[0] = "Rohatash";

Array Length

Array length represents the total number of elements in all the dimensions of the Array.

Basic array length

 Dim arrayA As Integer() = New Integer(7) {}

 Dim lengthA As Integer = arrayA.Length

 Console.WriteLine("Basic array length =" & lengthA)

 

Long array length

 Dim longLength As Long = arrayA.LongLength

 Console.WriteLine("Long array length =" & longLength)

Zero length array

Dim zero As Integer() = New Integer(-1) {}

Dim lengthZero As Integer = zero.Length

Console.WriteLine("zero length array =" & lengthZero)

 

Two-dimensional Length example

Dim two As Integer(,) = New Integer(5, 9) {}

        Console.WriteLine("First-dimensional GetLength array=" & two.GetLength(0))

        Console.WriteLine("Second-dimensional GetLength array=" & two.GetLength(1))

 

Example

 

This example defines Basic array length, Long array length, Zero length array, Two-dimensional Length example.

 

VB code

 

Module Module1

    Sub Main()

        Dim arrayA As Integer() = New Integer(7) {}

        Dim lengthA As Integer = arrayA.Length

        Console.WriteLine("Basic array length =" & lengthA)

 

        ' Long array length example.

        Dim longLength As Long = arrayA.LongLength

        Console.WriteLine("Long array length =" & longLength)

 

 

        ' Zero length array example.

        Dim zero As Integer() = New Integer(-1) {}

        Dim lengthZero As Integer = zero.Length

        Console.WriteLine("zero length array =" & lengthZero)

 

 

        ' Two-dimensional GetLength example.

        Dim two As Integer(,) = New Integer(5, 9) {}

        Console.WriteLine("First-dimensional GetLength array=" & two.GetLength(0))

        Console.WriteLine("Second-dimensional GetLength array=" & two.GetLength(1))

    End Sub

 End Module

 

OUTPUT

 

len1.gif

 

C# code
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace array_length

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arrayA = new int[8];

            int lengthA = arrayA.Length;

            Console.WriteLine("Basic array length =" +lengthA);

 

            // Long array length example.

            long longLength = arrayA.LongLength;

            Console.WriteLine("Long array length =" + longLength);

 

          

            // Zero length array example.

            int[] zero = new int[0];

            int lengthZero = zero.Length;

            Console.WriteLine("zero length array =" + lengthZero);

                              

         

            // Two-dimensional GetLength example.

            int[,] two = new int[6, 10];

            Console.WriteLine("First-dimensional GetLength array=" +two.GetLength(0));

            Console.WriteLine("Second-dimensional GetLength array=" +two.GetLength(1));

 

        }

    }

}

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    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.
Nevron Diagram
Become a Sponsor