Language: Arrays
This sample demonstrates features of arrays.
Featured Highlights
The application shows how to work with an array of value types, strings,
and an array of reference types, a custom Customer object. The Customer
object exposes public Id and Name fields. There are six command buttons
which execute various procedures.
-
Create Static Array葉his procedure will create an array
using the new {} syntax to load an array of values. The Array of group box
provides an option for loading the array with strings or objects (Customer)
instances. Once the array is loaded, it is passed to a support procedure,
DisplayArrayData, which enumerates through the array and puts the values in
Data As Loaded list box.
-
Sort葉his procedure loads the data using the same code as
in the Create Static Array example. However, once the data is loaded, the
shared method Sort from the Array class is applied the data is loaded into the
lstAfter list box control.
-
Reverse葉his procedure works just like Sort except it
sorts the data in the reverse order of which the data was loaded.
-
Binary Search用erforming a binary search requires that array
be sorted first. So after the data is loaded into the array, it is sorted, and
then a binary search is performed. The value provided in text box labeled
Search For is used as the search criteria. If the item is found, then its index
position is displayed. If the item is not found, the Array.BinarySearch
function returns the bitwise complement of where the item would
have been if it existed.
-
Create Dynamic Array葉his procedure uses the numeric value
provided in the text box txtLength to ReDim an array. It the loops through each
new element and displays an InputBox requested a value for the item. After
which, that data is loaded into a list box.
-
Create Matrix Array葉his procedure builds a static two
dimensional array that contains two columns, and three rows and then displays
it in a list box.
Note that in order to keep the code isolated, each array is built at the
procedure level each time you click one of the buttons.
Requirements
Requires the Trial or Release version of Visual Studio .NET Professional (or
greater).
Running the Sample
Changing the Array of option from Strings to Objects
will enable Field to use for Sorts which
controls whether an array of Customer objects is sorted by the Customer's Name
field or Id field. In order for sorting or searching of an arry of objects
to work, the object needs to implement the IComparable interface. This
interface exposes a single method CompareTo. CompareTo simply
evaluates the passed in object reference to itself using any criteria that
makes since to the object. If the object passed in equals the current
object, the 0 is returned. If the object is less than the current object,
return 1, otherwise -1. The Customer object uses its name field as the test
value to determine object equivalence. Optionally, the shared method
SetCompareKey can be called before a sort is performed to change the field
used.
Note that strings and primative types like integers are automatically
comparable.
Last Update: 12 July 2002