Arrays in Visual Basic

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

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));

         }

    }

}

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.