Array Manipulation using Array Class in Visual Basic .Net
The array class is the base class for all arrays in Visual Basic .Net which is defined in the System namespace. The Array class provides some properties and methods to manipulate arrays.
Properties of Array Class
The following are the major properties of the Array Class.
- IsFixedSize : To check whether the Array has a fixed size or not.
- IsReadOnly : To check whether the Array is read only or not.
- Length : Check the total number of elements of the array and get value to a 32-bit integer.
- Long Length : Check the total number of elements of the array and get value to a 64-bit integer.
- Rank : To get the number of dimensions of the array.
Methods of the Array Class
Following are the commonly used methods of Array Class in Visual Basic .Net.
Public Shared Sub Clear (array As Array, index As Integer, length As Integer)
Clears the array elements and set values to zero, null, or false according to the array type.
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)
Copies a specified length of elements to destination array from source array. The length is specified as a 32-bit integer.
Public Sub CopyTo (array As Array, index As Integer)
Copies whole elements from the current one-dimensional Array to the specified one-dimensional Array, starting at the specified destination Array index. The index is specified as a 32-bit integer.
Public Function GetLength (dimension As Integer) As Integer
Returns the number of elements in the specified dimension of the Array to a 32-bit integer.
Public Function GetLowerBound (dimension As Integer) As Integer
Returns the lower bound of the specified dimension in an Array.
Public Function GetUpperBound (dimension As Integer) As Integer
Returns the upper bound of the specified dimension in an Array.
Public Function GetType As Type
Returns the type of the current instance.
Public Function GetValue (index As Integer) As Object
Returns the value in a specified index from the array. The index is specified as the 32-bit integer.
Public Shared Function IndexOf (array As Array,value As Object) As Integer
Returns the index of a value in a specified array. If the value occurs more than once, the index of first one returns.
Public Shared Sub Reverse (array As Array)
To reverse the order of the specified one-dimensional array.
Public Sub SetValue (value As Object, index As Integer)
To set a value for the specified index position in an array.
Public Shared Sub Sort (array As Array)
To sort the elements of an one dimensional array.
Example Program
The below program includes all common used Array Class methods.
Module Module1 Sub Main() Dim list As Integer() = {34, 72, 13, 44, 25, 30, 10} Dim i, ind, e, sv, po, strt, en As Integer Dim copyarray(6) As Integer Console.WriteLine("Original Array is :") For Each i In list Console.WriteLine(i) Next i Console.WriteLine("1. Copy Array") Console.WriteLine("2. Reverse an Array") Console.WriteLine("3. Sort an Array") Console.WriteLine("4. Lower Bound") Console.WriteLine("5. Upper Bound") Console.WriteLine("6. Array Type") Console.WriteLine("7. Element of Index") Console.WriteLine("8. Index of Element") Console.WriteLine("9. Replace an element") Console.WriteLine("10. Set value") Console.WriteLine("11. Clear an Element") Dim op As Integer Dim a As Char Do Console.WriteLine() Console.WriteLine("Choose your operation: ") op = Console.ReadLine() Select Case op Case 1 Array.Copy(list, copyarray, 7) Console.WriteLine() Console.WriteLine("Copy of array: ") For Each i In copyarray Console.WriteLine(i) Next i Case 2 Array.Reverse(list) Console.WriteLine() Console.WriteLine("Reversed Array: ") For Each i In list Console.WriteLine(i) Next i Case 3 Array.Sort(list) Console.WriteLine() Console.WriteLine("Sorted Array Is") For Each i In list Console.WriteLine(i) Next i Case 4 Console.WriteLine() Console.WriteLine("Lower bound: {0}", list.GetLowerBound(0)) Case 5 Console.WriteLine() Console.WriteLine("Upper bound: {0}", list.GetUpperBound(0)) Case 6 Console.WriteLine() Console.WriteLine("Type of Array:{0}", list.GetType) Case 7 Console.WriteLine() Console.WriteLine("Element of Index: ") Console.WriteLine("Enter the Index: ") ind = Console.ReadLine() Console.WriteLine("Value of index: {0}", list.GetValue(ind)) Console.WriteLine() Case 8 Console.WriteLine("Index of Element") Console.WriteLine("Enter the Element") e = Console.ReadLine() Console.WriteLine("Index of the element {0}: {1}", e, Array.IndexOf(list, e)) Console.WriteLine() Case 9 Console.WriteLine("Replace an element") Console.WriteLine("Enter the element to insert:") sv = Console.ReadLine() Console.WriteLine("Enter the pos to insert ") po = Console.ReadLine() Console.WriteLine("Element {0} is replaced with {1}", list(po), sv) list.SetValue(sv, po) For Each i In list Console.WriteLine(i) Next i Console.WriteLine() Case 10 Console.WriteLine() Console.WriteLine("Enter the element:") Dim ele As Integer = Console.ReadLine() Console.WriteLine("Enter the position:") ind = Console.ReadLine() list.SetValue(ele, ind) Console.WriteLine("Set value:") For Each i In list Console.WriteLine(i) Next i Case 11 Console.WriteLine("Clear the array elements") Console.WriteLine("Enter the starting pos to clear:") strt = Console.ReadLine() Console.WriteLine("Enter the ending position to clear") en = Console.ReadLine() Console.WriteLine("Elements are cleared from {0} to {1}", strt, en) Array.Clear(list, strt, en) For Each i In list Console.WriteLine(i) Next i Case Else Console.WriteLine("Invalid Selection") End Select Console.ReadKey() Console.WriteLine("Do You Want To Continue Operations ? (y or n)") a = Console.ReadLine() Loop Until (a = "n") End Sub End Module