System.Array Class using VB.NET: Part 1

In this article I will explain you about the System.Array Class using VB.NET.
  • 7103
 

Arrays are one of the fundamental data structures in programming languages, providing a storage area for sequential data values. System.Array is the base class for all arrays in the common language runtime. It has methods for creating, manipulating, searching, and sorting the arrays we have talked about so far. System.Array implements several interfaces, like ICloneable, IList, ICollection, and IEnumerable. It also has many properties and methods to aid you in determining information about your array.

The Length property of the array returns the total number of elements in all of the array's dimensions.

The Rank property returns the rank the number of dimensions of the array, which is useful in multidimensional arrays.

You can use the Clear function to reset a range of array elements to zero or to a null reference.

The GetLowerBound function returns the lower bound of the specified dimension in an array. GetLowerBound(0) returns the lower bound for the indexes of the first dimension of an array, and GetLowerBound(Rankâ€"1) returns the lower bound for the indexes of the last dimension of an array.

The GetUpperBound function returns the upper bound of the specified dimension in an array. GetUpperBound(0) returns the upper bound for the indexes of the first dimension of an array, and GetUpperBound(Rankâ€"1) returns the upper bound for the indexes of the last dimension of an array.

Using the Copy function, you can copy a range of elements from one array, starting at the specified source index, and paste them into another array, starting at the specified destination index:

// Copy the first 2 elements from mySourceArray to myDestinationArray Array.Copy( mySourceArray, mySourceArray.GetLowerBound(0), myDestinationArray, myDestinationArray.GetLowerBound(0), 2);

There are three ways to enumerate array elements:

  • Using the foreach loop structure

  • Looping with a for loop along the length of the array

  • Using the GetEnumerator function and implementing IEnumerator

The given below example expalin these three enumeration methods

Example of Looping Through Array Class Objects

    Class Test
        Shared Sub TestForEach(ByRef myArray As Integer())
            For Each x As Integer In myArray
                Console.WriteLine(x)
            Next
        End
Sub

        Shared
Sub TestForWithLength(ByRef myArray As [Object]())
            For x As Integer = 0 To myArray.Length - 1
                Console.WriteLine(myArray(x))
            Next
        End
Sub

        Shared
Sub TestForEnum(ByRef myArray As System.Array)
            Dim myEnumerator As System.Collections.IEnumerator = myArray.GetEnumerator()
            Dim i As Integer = 0
            Dim cols As Integer = myArray.GetLength(myArray.Rank - 1)
            While myEnumerator.MoveNext()
                If i < cols Then
                    i += 1
                Else
                    Console.WriteLine()
                    i = 1
                End If
                Console.WriteLine(myEnumerator.Current)
            End While
        End
Sub

        Shared
Sub Main()
            ' an int array and an Object array
            Dim myIntArray As Integer() = New Integer(4) {5, 4, 3, 2, 1}
            TestForEach(myIntArray)
            ' an Object array
            Dim myObjArray As [Object]() = New [Object](4) {99, 98, 97, 96, 95}
            TestForWithLength(myObjArray)
            ' another object array
            Dim myObjArray2 As Array = Array.CreateInstance(Type.[GetType]("System.Object"), 5)
            For i As Integer = myObjArray2.GetLowerBound(0) To myObjArray2.GetUpperBound(0)
                myObjArray2.SetValue(i * i, i)
            Next
            TestForEnum(myObjArray2)
            Console.ReadLine()
        End Sub
    End
Class

Output of the above example

system.array1.gif
 

Conclusion

Hope this article would have helped you in understanding the System.Array Class using VB.NET

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.