This documentation is archived and is not being maintained.

Array Sample

Visual Studio 2008

This sample shows how to work with arrays of value types and arrays of reference types. The value type that is used is the Integer. The reference type that is used is a Customer class defined in the project. The Customer class exposes two public members, Id and Name.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

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, 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.

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.

Show: