VB.Net - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Arrays

VB.Net Arrays

shape Description

An Array is a Collection of Elements which are stored in continuous memory locations. All the elements in the array belong to the same datatype.Elements in the array start with the value '0' and  this element is called as Lowest or First Element in the array.Ending element will be called as Higher or Last element.Following are the types of arrays.

shape Conceptual figure

Declaration Of Arrays

shape Syntax

The following code shows you the syntax of an array.
Dim Array_Name() As Datatype = {Element[0],Element[1],..Element[N]}

shape Example

Following is a basic example to declare an array. [vbnet]Dim Names As String= {"Welcome","To","SPlessons"}[/vbnet]

Multi Dimensional Array

shape Description

Multi Dimensional arrays are used to declare two or more dimensions for an array.Following are the syntax and basic example to describe multi dimensional array.
Dim Array_Name(,) As Datatype = {{Element[0],Element[0]},{Element[1],Element[1]},..{Element[N],Element[N]}
[vbnet]Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}[/vbnet]

shape Example

[vbnet] Module Module1 Sub Main() ' an array with 5 rows and 2 columns Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}} Dim i, j As Integer ' output each array element's value ' For i = 0 To 4 For j = 0 To 1 Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j)) Next j Next i Console.ReadKey() End Sub End Module [/vbnet] Output: Following is a 2-dimensional array output.

Jagged Arrays

shape Description

Jagged Array means Array of Arrays. That means elements in the array are also arrays.Following is a syntax to declare a jagged array.
Dim scores As Integer()() = New Integer(5)() { }

shape Example

[vbnet]Module Module1 Sub Main() 'a jagged array of 5 array of integers Dim a As Integer()() = New Integer(4)() {} a(0) = New Integer() {0, 0} a(1) = New Integer() {1, 2} a(2) = New Integer() {2, 4} a(3) = New Integer() {3, 6} a(4) = New Integer() {4, 8} Dim i, j As Integer ' output each array element's value For i = 0 To 4 For j = 0 To 1 Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i)(j)) Next j Next i Console.ReadKey() End Sub[/vbnet] Output: Following is an output in console.

shape Properties

The following table shows you the different properties of the arrays.
Properties Description
Is Fixed Size Used to Check Wheater Array Is in Fixed size or not
Length Used to Get total number of elements in array(32bit Integer)
Rank Used to Get number of dimensions
LongLength Used to Get total number of elements in array(64bit Integer)
IsReadOnly Used to Check Wheater Array is in Read-Only or not

shape Methods

The following are the different properties of the arrays.
Methods Description
Clear (Array, Index, Length) Used to elements in the Array
Sort (Array) Used to sort the elements in one-dimensional Array
Reverse (Array) Used to get the reverse order of  elements in Array
Copy (sourceArray,destinationArray,Length) Used to copy one Array to another Array based on length
Get Type Used to get the type of element
SetValue (Value, Index) Used to sets the elements at the specified position(32bit Integer)
Get Value Gets the value at the specified position (32bit Integer)
Copy To (Array, Index) Used to copy current one-dimensional array to the specified one-dimensional array
Get Length (Dimensions) Used to represent the number of elements (32bit Integer)

Summary

shape Points

  • Redim statement is used to declare dynamic array.
  • Preserve is a keyword in Redim statement syntax.
  • Rectangular array is an another name of multi dimensional array.