Posted on 3rd May 2010 by Admin in Visual Basic 2010
Array, LINQ, Visual Basic 2010
This sample shows how you can use collectioninitializersyntax to create arrays and collections. The Customer and Order classes create a parent-child relationship to demonstrate nested collectioninitializersyntax. TheGenericAddmodule contains extension methods that enable you to use nested collectioninitializersyntax to add Customer and Order objects to collections.
The buttons and radio button options perform the following tasks:
- Create an Array – Creates an array of integers, strings, or customers using collectioninitializersyntax to load an initial set of values. Once the array is loaded, the results are displayed in the resultsListBox.
- Create a Collection-Creates a collection of integers, strings, or customers using collectioninitializersyntax to load an initial set of values. Once the collection is loaded, the results are displayed in the resultsListBox.
- Create Customers with Orders-Creates a collection of customers and orders using nested collectioninitializersyntax to load an initial set of values. Once the collection is loaded, the customers are displayed in the resultsListBox. You can click on a customer to see the related orders displayed in the detailsListBox.
- Create aDictionary -Creates a dictionary using nested collection initializer syntax to load an initial set of values. Once the collection is loaded, the keys are displayed in the resultsListBox. You can click on a key to see the related value displayed in the detailsListBox.
- Query a Collection-Creates a collection of integers and a collection of customers and uses LINQ to join the two collections based on the Id of the customer. The results of the query are displayed in the resultsListBox.
Download Project
Posted on 3rd May 2010 by Admin in Visual Basic 2010
Array, Visual Basic 2010
TheCustomerclass implements theIComparableinterface, which is required forSortandBinarySearchfunctionality.Customerobjects can be sorted by either theNameorIdproperty. Changing theArray ofoption fromStringstoObjectsenables theField to use for Sortscontrol that determines whether an array ofCustomerobjects is sorted by the Customer’sNamefield orIdproperty. The shared methodSetCompareKeyof theCustomerclass can be called before a sort is performed to change the field that is used.
The six buttons perform the following tasks:
- Create Static ArrayCreates 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, seeDim Statement (Visual Basic).
- SortLoads the data by using the same code as in theCreate Static Arrayprocedure. Once the data is loaded, the shared methodSortof theArray Classclass is applied, and then the data is loaded into thelstAftercontrol. Strings and primitive types like integers are automatically comparable. TheIComparableinterface is implemented on theCustomerclass to enable sorting.
- ReverseThe procedure uses theReversemethod to reverse the order of the elements in the array. (Reversedoes not do a reverse sort of the elements; it reverses the elements in the array.)
- Binary SearchPerforming 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 theBinarySearchmethod. The value provided in the text-box control labeledSearch Foris used as the search criteria. If the item is found, its index position is displayed. If the item is not found, theBinarySearchmethod returns the bitwise complement of where the item would have been if it existed.
- Create Dynamic ArrayUses the numeric value provided in the text-box controltxtLengthtoReDiman 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 ArrayBuilds a two-dimensional array that contains two columns and three rows, and then displays it in a list box.
Download Project