Arrays in VB.NET

In this article I will explain you about Arrays in VB.NET.
  • 6397

Arrays: An array in VB.NET is simply a set of sequential memory locations that can be accessed using either indices or references. A mechanism in VB.NET prevents a program from writing outside the bounds of an array and destroying the contents of memory in the process: The declaration of an array simply sets aside the requisite block of memory and treats the name of the array as a synonym for the address of the block's beginning.

On our behalf, .NET Framework handles memory allocation behind the scenes. Arrays can be multidimensional, but their elements must be of the same base type. Arrays can store integers, strings, and any other type of object, including references and other arrays.

Arrays and Array Initializations

In VB.NET, before using an array, you must declare it, providing two important pieces of information:

  • The name of the array

  • The type of data to be stored in it

Arrays may be declared in VB.NET using the format below:

           
Dim strNames As String()

As with other objects in VB.NET, the declaration does not allocate memory for the array data but rather allocates memory for a reference to the array. Memory to contain the array data must be allocated from dynamic memory using statements such as the one below:


           
Dim myArray As Integer() = New Integer(14) {}

VB.NET saves space until you explicitly assign a value to a reference type. For value types, however, the space is allocated immediately (see the example in the "System.Array Class" section).

The two statements above simultaneously declare the name of the array and cause memory to be allocated to contain it. The references are allocated but do not refer to anything until the array is actually assigned data.

It is not necessary, however, to combine these two processes. You can execute one statement to declare the array and another statement to allocate the memory:


           
Dim strNames As String()
            // some code
           
Dim myArray As Integer() = New Integer(24) {}

Causing memory to be set aside to contain the array data is commonly referred to as instantiating the array objectâ€"or creating an instance of the array object. If you prefer to declare and instantiate the array at different points in your program, you can use the syntax above.

This pattern is very similar to the declaration and instantiation of all objects in VB.NET. This is the general syntax for declaring and instantiating an array:


           
Dim nameOfArray As typeOfElements() = New typeOfElements(sizeOfArray - 1) {}        

The following code fragment illustrates another interesting aspect of arrays in VB.NET:


            
For cnt As Integer = 0 To myArray.Length - 1
            myArray(j) = j
        Next

All array objects in VB.NET have a Length property that can be accessed to determine the number of elements stored in the array.

Listing 20.1 illustrates the use of single-dimensional arrays.

Listing 20.1: Outputting an array of strings to the Console


Imports System.Collections.Generic
Imports System.Linq
Imports System.Text 
Namespace array
    Class Program
        Shared Sub Main(ByVal args As String())
            Console.WriteLine("Enter a sentence")
            Dim str1 As [String] = Console.ReadLine()
            Console.WriteLine(""
            'Iterate through the items of array args using foreach
            For Each s As Char In str1
                Console.WriteLine(s)
            Next
 
            Console.WriteLine("")
            'Declare array strNames
            Dim strNames As String() = {"Ahmet", "Mustafa", "Mehmet", "Mahmut"
            'Iterate through the items of array strNames
            For i As Integer = 0 To strNames.Length - 1
                Console.WriteLine("strNames[{0}] = {1}", i, strNames(i))
                Console.ReadLine()
            Next
        End Sub
    End Class
End Namespace

Output Window

array.gif
 

Conclusion

Hope this article would have helped you in understanding Arrays in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.