May 13, 2021 vb.net
Arrays store a fixed-size sequence of elements of the same type.
Arrays are used to store data collections, but it is often more useful to think of arrays as collections of variables of the same type.
All arrays consist of successive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.
To VB.Net array in a single file, you can use theIm statement. F or 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 array elements when you declare an array. F or 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}
You can store and access elements in an array by using the index of the array. T he 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 results:
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 are arrays that can be dimensioned and redefined according to the needs of the program.
You can use the ReDim statement to declare a dynamic array.
The syntax of the ReDim statement:
ReDim [Preserve] arrayname(subscripts)
The Preserve keyword helps you keep data from an existing array when you resize it.
Arrayname is the name of the array to re-dimension.
Subscripts specifies a new dimension.
Module arrayApl Sub Main() Dim marks() As Integer ReDim marks(2) marks(0) = 85 marks(1) = 75 marks(2) = 90 ReDim Preserve marks(10) marks(3) = 80 marks(4) = 76 marks(5) = 92 marks(6) = 99 marks(7) = 79 marks(8) = 75 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 results:
0 85 1 75 2 90 3 80 4 76 5 92 6 99 7 79 8 75 9 0 10 0
Vb. N et allows multi-dimensional arrays. M ulti-dimensional arrays are also known as rectangular arrays.
You can declare an array of two-dimensional strings:
Dim twoDStringArray(10, 20) As String
Alternatively, a 3-dimensional array of integer variables:
Dim threeDIntArray(10, 10, 10) As Integer
The following program demonstrates creating and using two-dimensional arrays:
Module arrayApl 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
When the above code is compiled and executed, it produces the following results:
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 Jagged array is an array of arrays. T he following code shows an irregular array declared called score of Integers:
Dim scores As Integer()() = New Integer(5)(){}
The following example illustrates the use of irregular arrays:
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 results:
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 VB.Net class of all arrays in the system. I t is defined in the system namespace. A rray classes provide a variety of properties and methods for handling arrays.
The following table provides some of the most commonly used properties in array classes:
SN | The name and description of the property |
---|---|
1 |
IsFixedSize
Gets a value indicating whether the Array has a fixed size.
Gets a value that indicates whether the array has a fixed size. |
2 |
IsReadOnly
Gets a value indicating whether the Array is read-only.
Gets a value that indicates whether 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.
Gets a 32-bit integer that represents the total number of elements in all 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.
Gets a 64-bit integer that represents the total number of elements in all dimensions of the array. |
5 |
Rank
Gets the rank (number of dimensions) of the Array.
Gets the rank (dimensional) of the array. |
The following table provides some of the most commonly used Array class methods:
SN | Method name and description |
---|---|
1 |
Public Shared Sub Clear (array As Array, index As Integer, length As Integer)
Depending on the element type, set the zero of an array element in a range to false, or empty. |
2 |
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)
Copy elements within a certain range from arrays to the first element and paste them into another array that starts with the first element. T he length is specified as a 32-bit integer. |
3 |
Public Sub CopyTo (array As Array, index As Integer)
All elements that take the current one-dimensional array to the specified one-dimensional array from the index of the specified target array. T he index is specified as a 32-bit integer. |
4 |
Public Function GetLength (dimension As Integer) As Integer
Gets a 32-bit integer that represents the number of elements in the specified dimensionality of the array. |
5 |
Public Function GetLongLength (dimension As Integer) As Long
Gets a 64-bit integer that represents the number of elements in the specified dimensionality of the array. |
6 |
Public Function GetLowerBound (dimension As Integer) As Integer
Gets the dimensions specified by the lower bound in the array. |
7 |
Public Function GetType As Type
Gets the type of the current instance (inherited from Object). |
8 |
Public Function GetUpperBound (dimension As Integer) As Integer
Gets the size specified by the upper limit in the array. |
9 |
Public Function GetValue (index As Integer) As Object
Gets the value that specifies the location in the one-dimensional array. T he index is specified as a 32-bit integer. |
10 |
Public Shared Function IndexOf (array As Array,value As Object) As Integer
Search for the specified object and return the index in the entire one-dimensional array that first appears. |
11 |
Public Shared Sub Reverse (array As Array)
Reverse the order of the elements throughout the one-dimensional array. |
12 |
Public Sub SetValue (value As Object, index As Integer)
An element that sets a value at a specified location in a one-dimensional array. T he index is specified as a 32-bit integer. |
13 |
Public Shared Sub Sort (array As Array)
Use sorted IComparable to implement elements in the array for each element in the entire one-dimensional array. |
14 |
Public Overridable Function ToString As String
Returns a string that represents the current object(inherited from Object). |
For a complete list of Array class properties and methods, see the Microsoft documentation.
The following program demonstrates some of the Aray class methods used:
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: ") For Each i In list Console.Write("{0} ", i) Next i Console.WriteLine() ' reverse the array Array.Reverse(temp) Console.Write("Reversed Array: ") For Each i In temp Console.Write("{0} ", i) Next i Console.WriteLine() 'sort the array Array.Sort(list) Console.Write("Sorted Array: ") For Each i In list Console.Write("{0} ", i) Next i Console.WriteLine() Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Original Array: 34 72 13 44 25 30 10 Reversed Array: 10 30 25 44 13 72 34 Sorted Array: 10 13 25 30 34 44 72