How to Work with a VB.NET Array?

vb.net arrayAn array is a collection of variables —like numbers, strings or characters — of the same datatype. The datatype of the array is not the same as the type of the elements or variables it contains. An array forms as an object holding a sequence of data. It is a collection of data that you can iterate through; you can store value in, you can retrieve it out and you can assign values to it. You can use an array to group items that are logically related, such as the number of books in different aisles of a library. An array will allow you to refer to each element in it by the same name as the array, and an index number. The elements are contiguous from the index value 0 to the highest index value. You can declare an array just like any other variable in vb.net.

New to VB.NET? Learn how to code in VB.NET through a course in Udemy.com

Declaring an array variable in visual basic:

 

Example 1:

‘declare an array library, which is going to contain 7 elements of type Integer.

Dim library(6) As integer

Or

 

Dim library(0 To 6) As Integer

 

Example 2:

‘an array that holds string values

Dim names(4) As String

 

Assigning Values to a VB.NET Array

In example 1, we defined an array of integers. The numbers in the parenthesis are the boundaries of the array. In this example, the array will have 7 items; starting from the index number 0 all the way through to number 6.

In order to access an individual element of the array, you use the name of the array variable — so in this case the word ‘library’ — along with the index number of the item we want to access.

Consider the following example:

Sub Main()

Dim library(0 to 6) As integer

library(0)  = 500        ‘accessing and assigning value to element at index 0 and so on

library(1) = 550

library(2) = 400         ‘the library array at index 2 equals to 400

library(3) = 450

library(4) = 600

library(5) = 650

library(6) = 700

‘retrieve the total length of the array and print it out to the console

Console.WriteLine(“the number of  aisles in the library:”  &  library.Length)

‘iterate through all the elements from index 0 to 6

For index = 0 to library.Length-1

‘assign the value of the element to integer variable books at every iteration

books As Integer = library(index)

‘print the aisle number and the number of books at that aisle to the console

Console.WriteLine(“Aisle number” & index + 1 & “ has” &  books & “books”)

Next

Console.ReadLine()

End Sub

OUTPUT:

the number of aisles in the library: 7

Aisle number 1 has 550 books

Aisle number 2 has 500 books

Aisle number 3 has 400 books

Aisle number 4 has 450 books

Aisle number 5 has 600 books

Aisle number 6 has 650 books

Aisle number 7 has 700 books

It is important to remember that arrays are always zero based. This means you always count the array with 0 as the starting element.

You cannot expand or shrink the length of an array after it has been defined. Doing so will throw an array out of bound exception. In the above example, the for iteration runs from 0 to library.Length-1, which means 0 to 7-1. Without the -1, the code will throw an error.

In the .NET framework, a single quotation mark is used for commenting out any line of code or for writing notes/comments. Learn how to work with .NET through a course in Udemy.com

An array can also be declared and assigned values in a single line of code. The array from the preceding example can also be defined in the following way.

‘an array that holds 7 integer values

Dim library() As Integer  = {550,  500,  400,  450,  600,  650,  700}

The same method can be used for defining an array of elements of other data types.

‘an array that holds 4 string values

Dim names() As String = { “Anne”,  “Bob”, “Alex”,  “David”}

Multidimensional Array in VB.NET

The arrays that we have so far declared are single dimensional arrays — it represents data stored in a linear form. Vb.net allows multidimensional array.

You can declare a 2-D array as follows:

Dim mArray(15,25) As String

A 3-D array can be declared as follows:

Dim  threeDArray(12, 15, 17) As Integer

The following example shows how to access a multidimensional array:

Sub Main()

‘ declare an array with 4 rows and 2 columns

Dim arr(,) As Integer = {{0,0}, {3,4}, {6,7}, {8,9}}

Dim x, y As Integer

‘ print each element of the array to console

For  x = 0 To 3

For  y =  0 To 1

Console.WriteLine(“arr[{0},{1}] = {2}”, x, y, arr(x, y))

Next  y

Next x

Console.ReadLine()

End Sub

End Module

OUTPUT:

arr[0, 0] = 0

arr[0, 1] = 0

arr[1, 0] = 3

arr[1, 1] = 4

arr[2, 0] = 6

arr[2, 1] = 7

arr[3, 0] = 8

arr[3, 1] = 9

VB.NET Array Properties

The class Array defined in the namespace is the base class for all arrays in VB. It provides various methods and properties that can be implemented with arrays. Just as any object oriented language, the methods can be accessed using the dot operator.

Learn about the various functionalities of .Net through a udemy course

Some common array properties used in vb.net are:

  • IsReadOnly : provides a value that tells if the array is read-only
  • LongLength : provides a 64-bit integer that gives the total number of elements in all dimensions of the Array.
  • IsFixedSize : provides a value indicating if the array has a fixed size
  • Length: provides a 32-bit integer that represents the total number of elements in all dimensions of the Array.
  • Rank: Provides the total number of dimensions of the array

Some common array methods used in vb.Net are:

  • Sub CopyTo(array As Array, index As Integer)

The function of this method is to copy all the items  of a single dimensional array to the destination single dimensional array.

  • Function GetLength ( dimension As Integer) As Integer

This method returns an integer, which is the number of elements in the specified dimension of the Array.

  • Function GetValue (index As Integer) As Object

This method provides the value at the specified position in a single dimensional array

  • Shared Sub Reverse( array As Array)

Reverses the sequence of items in the entire single dimensional Array