Visual Basic.net L7
Visual Basic.net L7
Array is a derived or user defined data type, which is used to store more than one
values of same data type. It means, it is used to store homogenous types of data.
Hence, in the case, variable name is same for more than one values of same data
type.
Syntax: -
Dim a (10) as integer
Dim b (10) as single
Dim c (10) as string
To declare an array in VB.Net, you use the Dim statement. For example,
Dim intData(30) ' an array of 31 elements
Dim strData(20) As String ' an array of 21 strings
Dim twoDarray(10, 20) As Integer 'a two dimensional array of integers
Dim ranges(10, 100) 'a two dimensional array
You can also initialize the array elements while declaring the array. For example,
Dim intData() As Integer = {12, 16, 20, 24, 28, 32}
Dim names() As String = {"Karthik", "Sandhya", _
"Shivangi", "Ashwitha", "Somnath"}
Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}
There are two types of arrays: -
1. Single Dimensional Array
2. Multi Dimensional Array
When one dimension is mentioned with an array variable than it is known as Single
Dimensional Array.
Syntax: -
Dim variable (size) As Data type
Example: -
The memory is allocated by the array variable name either horizontally or vertically
and whole space is divided into different blocks according to the given size. Each
block is given the unique numerical number (starts from 0), known as INDEX
NUMBER / BLOCK NUMBER / POCKET NUMBER. Each block has the same size
and has capacity to hold one number at a time.
Example: -
0 1 2 3 4
Now, the value is assigned or retrieved to or from an array variable, by using the
variable name and index number.
_____________________________________
Assignment Retrieve
_____________________________________
a (2) = 10 | print a (2)
a (4) = 40 | print a (4)
Syntax: -
Example: -
Dim a (0 to 10) As Integer
0 1 2 3 4 5 6 7 8 9 10
When more than one dimension is mentioned in an array variable, then it is known as
Multi – Dimensional array.
When two dimensions is mentioned with an array variable, and then it is known as
Double Dimensional array.
Syntax: -
The first ―size stands for row value and the second size stands for column value.
The memory space is allocated by the DDA variable name and whole space is divided
into different row and column according to specified dimensions. Each row and
column is numbered by an index number, starts from Zero (0).
Example: -
The combination of row and column is known as cell. So, the data is stored in DDA
according to cell wise.
Dim a (0 to 2, 0 to 2) As Integer
The elements in an array can be stored and accessed by using the index of the array.
The following program demonstrates this −
Module arrayApl
Sub Main()
Dim n(10) As Integer ' n is an array of 11 integers '
Dim i, j As Integer
' initialize elements of array n '
For i = 0 To 10
n(i) = i + 100 ' set element at location i to i + 100
Next i
' output each array element's value '
For j = 0 To 10
Console.WriteLine("Element({0}) = {1}", j, n(j))
Next j
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
Element(0) = 100
Element(1) = 101
Element(2) = 102
Element(3) = 103
Element(4) = 104
Element(5) = 105
Element(6) = 106
Element(7) = 107
Element(8) = 108
Element(9) = 109
Element(10) = 110
Dynamic Arrays
Dynamic arrays are arrays that can be dimensioned and re-dimensioned as par the
need of the program. You can declare a dynamic array using the ReDim statement.
Syntax for ReDim statement −
ReDim [Preserve] arrayname(subscripts)
Where,
• The Preserve keyword helps to preserve the data in an existing array, when
you resize it.
• arrayname is the name of the array to re-dimension.
• subscripts specifies the new dimension.
Module arrayApl
Sub Main()
Dim marks() As Integer
ReDim marks(2)
marks(0) = 85
marks(1) = 75
marks(2) = 90
For i = 0 To 10
Console.WriteLine(i & vbTab & marks(i))
Next i
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
0 85
1 75
2 90
3 80
4 76
5 92
6 99
7 79
8 75
9 0
10 0
Multi-Dimensional Arrays
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
When the above code is compiled and executed, it produces the following result −
a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8
Jagged Array
A Jagged array is an array of arrays. The following code shows declaring a jagged
array named scores of Integers −
Dim scores As Integer()() = New Integer(5)(){}
The following example illustrates using a jagged array −
Module arrayApl
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
End Module
When the above code is compiled and executed, it produces the following result −
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
The Array class is the base class for all the arrays in VB.Net. It is defined in the
System namespace. The Array class provides various properties and methods to work
with arrays.
The following table provides some of the most commonly used properties of
the Array class −
1 IsFixedSize
Gets a value indicating whether the Array has a fixed size.
2 IsReadOnly
Gets a value indicating whether the Array is read-only.
3 Length
Gets a 32-bit integer that represents the total number of elements in all the
dimensions of the Array.
4 LongLength
Gets a 64-bit integer that represents the total number of elements in all the
dimensions of the Array.
5 Rank
Gets the rank (number of dimensions) of the Array.
The following table provides some of the most commonly used methods of
the Array class −
Example
The following program demonstrates use of some of the methods of the Array class:
Module arrayApl
Sub Main()
Dim list As Integer() = {34, 72, 13, 44, 25, 30, 10}
Dim temp As Integer() = list
Dim i As Integer
Console.Write("Original Array: ")