Array Sample

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To view the sample documentation

  1. Locate this sample on the MSDN Web site, and then download and open the sample.

  2. In Solution Explorer, double-click the Documentation folder.

  3. If you are using Visual Basic Express Edition, right-click ReadMe.htm in the Documentation folder. Click View in Browser.

  4. If you are using another version of Visual Basic, double-click ReadMe.htm in the Documentation folder.

Demonstrates

The Customer class implements the IComparable interface, which is required for Sort and BinarySearch functionality. Customer objects can be sorted by either the Name or Id property. Changing the Array of option from Strings to Objects enables the Field to use for Sorts control that determines whether an array of Customer objects is sorted by the Customer's Name field or Id property. The shared method SetCompareKey of the Customer class can be called before a sort is performed to change the field that is used.

The six buttons perform the following tasks:

  • Create Static Array   Creates an array using the {} syntax to load an array of values. Once the array is loaded, a support procedure, DisplayArrayData, enumerates the array and puts the values in a list. For more information about declaring and initializing arrays, see Dim Statement (Visual Basic).

  • Sort   Loads the data by using the same code as in the Create Static Array procedure. Once the data is loaded, the shared method Sort of the Array Class class is applied, and then the data is loaded into the lstAfter control. Strings and primitive types like integers are automatically comparable. The IComparable interface is implemented on the Customer class to enable sorting.

  • Reverse   The procedure uses the Reverse method to reverse the order of the elements in the array. (Reverse does not do a reverse sort of the elements; it reverses the elements in the array.)

  • Binary Search   Performing a binary search requires that the elements in the array be sorted. After the data is loaded into the array it is sorted, and then a binary search is performed by using the BinarySearch method. The value provided in the text-box control labeled Search For is used as the search criteria. If the item is found, its index position is displayed. If the item is not found, the BinarySearch method returns the bitwise complement of where the item would have been if it existed.

  • Create Dynamic Array   Uses the numeric value provided in the text-box control txtLength to ReDim an array. It then loops through each new element and displays an input box requesting a value for the item. When all the values have been obtained, they are displayed.

  • Create Matrix Array   Builds a two-dimensional array that contains two columns and three rows, and then displays it in a list box.