Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Everyday Use of Generics : Page 2

Generics aren't a Ferrari that you only take out for special occasions; they're more like your trusty pickup, perfectly suited for everyday use.


advertisement
Data Binding With Generic Collections
One common programming task is displaying data to users. Generic collections, coupled with the latest databinding features in Visual Studio 2005, provide a quick and easy way to display data.

Generic collections, coupled with the latest data binding features, provide a quick and easy way to display data to users.
To try this, create a new form. Add a TextBox named NameTextBox, a button named AddButton, and a ListBox named NamesListBox to the form. Insert the following declaration into the form code:

Private NameList As New List(Of String)

In the Click event for the button, add the following code:

NameList.Add(NameTextBox.Text) NameTextBox.Text = String.Empty NamesListBox.DataSource = Nothing NamesListBox.DataSource = NameList

The first line adds the value entered into the textbox to the List. The second line clears the textbox so users can enter another value. The last two lines set up the binding.

Author's Note: The DataSource did not seem to reset to the latest information from the List unless it was first set to Nothing.

Run the resulting application. Type a name and click "Add." The name should appear in the ListBox. Repeat this step to add each name to the ListBox.

Next you'll display the data in sorted order using one of these techniques:

  1. Sort the generic List.
  2. Use a generic SortedList.
Both ways have pros and cons.

To sort the List, add a button named SortButton to the form and add the following code to the Click event for that button:



NameList.Sort() NamesListBox.DataSource = Nothing NamesListBox.DataSource = NameList

The first line performs the sort and the second two lines rebind the ListBox. This is necessary to ensure the ListBox contents reflect the sorted data.

Run the application. Type a name and click "Add." Repeat this several times to create a list of names that are in no particular order. Now click "Sort" to view the sorted list.

The Sort method performs an ascending sort on the contents of the List, assuming that the data type of the List has a default sort comparer. A comparer compares two values; sorting algorithms require comparers to determine whether an item should be sorted ahead of or behind another item. Strings and other basic data types have a default sort comparer. After the list is sorted, you can reverse the sort order using the List.Reverse method.

If you then add items to the List after it has been sorted, any newly added items will not appear in sorted order. You will need to resort after adding each item.

To define a list that will remain sorted as you add items, use the generic SortedList instead. To try this out, change the declaration of the List(of T) to be SortedList(Of TKey, TValue):

Private NameList As New SortedList(Of String, String)

The SortedList sorts on a unique key value, so it requires two type parameters. The first type parameter is the type of the unique key. The second parameter is the value.

The unique key requirement is the primary drawback of using the SortedList; you must assign a unique key to every value and you can sort only on that key (not on the value). However, the positive aspect of a SortedList is that it stays sorted. You don't need to resort after adding each entry.

To continue trying out the SortedList, you need to change the code that adds items to the List to assign a key as well. For example:

NameList.Add(NameTextBox.Text, NameTextBox.Text) NameTextBox.Text = String.Empty NamesListBox.DisplayMember = "Value" NamesListBox.DataSource = New BindingSource(NameList, Nothing)

This example uses the same text as the key and the value. Notice how the data binding code changed in this code. The NameList now has a Key and a Value, so you need to define which will be displayed in the list using the DisplayMember property of the ListBox. And because the binding is more complex, you can no longer simply assign the NameList as the DataSource. You must instead create a new BindingSource, passing the NameList as a parameter.

Because the keys must be unique, the example generates an error if a user types in a duplicate entry, so the example requires additional exception handling to cover for that condition:

If NameList.ContainsKey(NameTextBox.Text) Then MessageBox.Show("The entered item is already on the list") Else NameList.Add(NameTextBox.Text, NameTextBox.Text) NameTextBox.Text = String.Empty NamesListBox.DisplayMember = "Value" NamesListBox.DataSource = New BindingSource(NameList, Nothing) End If

Run the application. Type a name and click "Add." Repeat this several times and you will notice that the ListBox remains sorted. Type in a name that already exists and you should see your error message.

The default comparer for the SortedList sorts in ascending order. If you want to sort in descending order, you need to write your own generic comparer method as described in the next section.

Use data binding to display the contents of a List to the user as a ListBox or ComboBox. Use the List's Sort and Reverse methods to sort on possible repeating values. Use the SortedList instead if you:

  • Want the list to sort automatically as you add values
  • Can assign a unique key to every value
  • Want the List sorted by that key and not the value


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap