Previous section   Next section

9.3 System.Array

VB.NET implements arrays with the class System.Array. The Array class has a number of useful methods. Table 9-1 shows a few of the more important methods and properties of the System.Array class.

Table 9-1. Useful methods and properties of System.Array

Method or property

Description

Clear( )

Public shared method that sets a range of elements in the array to zero or to a null reference

Copy( )

Overloaded public shared method that copies a section of one array to another array

IndexOf( )

Overloaded public shared method that returns the index (offset) of the first instance of a value in a one-dimensional array

IsFixedSize

Public property that returns a value indicating whether the array has a fixed size

LastIndexOf( )

Overloaded public shared method that returns the index of the last instance of a value in a one-dimensional array

Length

Public property that returns the length of the array

Reverse( )

Overloaded public shared method that reverses the order of the elements in a one-dimensional array

Rank

Public property that returns the number of dimensions of the array

Sort( )

Overloaded public shared method that sorts the values in a one-dimensional array

The Array class's shared methods, Reverse( ) and Sort( ), make manipulation of the objects within the array very easy. Note, however, that to reverse or sort the elements of the array, they must be of a type that implements the IComparable interface, described in Chapter 8. The .NET Framework includes the String class, which does implement this interface, so we'll demonstrate both Reverse( ) and Sort( ) with Strings. The complete listing is shown in Example 9-9, followed by the output and analysis.

Example 9-9. Sort( ) and Reverse( ) methods of Array
Option Strict On
Imports System

Namespace ReverseAndSort
    Class Tester

        Public Shared Sub DisplayArray(ByVal theArray( ) As Object)
            Dim obj As Object
            For Each obj In theArray
                Console.WriteLine("Value: {0}", obj)
            Next obj
            Console.WriteLine(ControlChars.Lf)
        End Sub 'DisplayArray

        Public Sub Run( )
            Dim myArray As [String]( ) = {"Who", "is", "John", "Galt"}

            Console.WriteLine("Display myArray...")
            DisplayArray(myArray)

            Console.WriteLine("Reverse and display myArray...")
            Array.Reverse(myArray)
            DisplayArray(myArray)

            Dim myOtherArray As [String]( ) = _
              {"We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident"}

            Console.WriteLine("Display myOtherArray...")
            DisplayArray(myOtherArray)

            Console.WriteLine("Sort and display myOtherArray...")
            Array.Sort(myOtherArray)
            DisplayArray(myOtherArray)
        End Sub 'Run

        Public Shared Sub Main( )
            Dim t As New Tester( )
            t.Run( )
        End Sub 'Main
    End Class 'Tester

End Namespace 'ReverseAndSort

Output:
Display myArray...
Value: Who
Value: is
Value: John
Value: Galt

Reverse and display myArray...
Value: Galt
Value: John
Value: is
Value: Who

Display myOtherArray...
Value: We
Value: Hold
Value: These
Value: Truths
Value: To
Value: Be
Value: Self
Value: Evident

Sort and display myOtherArray...
Value: Be
Value: Evident
Value: Hold
Value: Self
Value: These
Value: To
Value: Truths
Value: We

Example 9-9 begins by creating myArray, an array of strings, containing the words:

"Who", "is", "John", "Galt"

This array is displayed, and then passed to the Array.Reverse( ) method, where it is displayed again to see that the array itself has been reversed:

Value: Galt
Value: John
Value: is
Value: Who

Similarly, the example creates a second array, myOtherArray, containing the words:

"We", "Hold", "These", "Truths",
"To", "Be", "Self", "Evident", 

which is passed to the Array.Sort( ) method. Then Array.Sort( ) happily sorts them alphabetically:

Value: Be
Value: Evident
Value: Hold
Value: Self
Value: These
Value: To
Value: Truths
Value: We

The method to display the strings has been made somewhat generic by declaring the type passed in to be an array of objects:

Public Shared Sub DisplayArray(ByVal theArray( ) As Object)

The DisplayArray( ) method iterates through the array of objects, passing each to WriteLine(). Since WriteLine( ) calls ToString( ) on objects, and since every object (including String) supports ToString( ), declaring the temporary variable obj to be of type Object works very well. Using objects has the advantage that you can reuse your DisplayArray( ) method with arrays of other types of objects, once you know how to implement the IComparable interface. Implementing interfaces is described in Chapter 8, and the IComparable interface, which is used with strings, is described in Chapter 10.


  Previous section   Next section
Top