RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Everyday Use of Generics : Page 3

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.

Writing Generic Methods
Generics provide a way for you to write code that can work with any data type, such as strings, integers, business objects, etc. You then specify the data type that you want to use when you call the method.

By writing a generic method, you can write code one time, and use in with every form and with any business object.
Since writing comparers for sorting is not such an everyday activity, I'll hold that until the end of this section. Instead, I'll start with more common functionality, such as a Save option on a form.

To really see the benefit of writing generic methods, the sample application will need to have a more complex data type, not just strings or integers (though strings and integers will of course work). Plus it will need more than one type to demonstrate how the generic method works with multiple types. The sample presented here creates Person and Task classes along with forms for entering person and task information.

Start by creating a business object. For this example, build a Person class with properties for LastName and FirstName:

   Private _LastName As String
   Public Property LastName() As String
         Return _LastName
      End Get
      Set(ByVal value As String)
         _LastName = value
      End Set
   End Property
   Private _FirstName As String
   Public Property FirstName() As String
         Return _FirstName
      End Get
      Set(ByVal value As String)
         _FirstName = value
      End Set
   End Property
Add a Save method to the Person class. For purposes of this example, it does not need to access a database and actually save the values, just display a message.

   Public Sub Save()
      ' just to see how this works…
      MsgBox("Got to the Save: " & LastName)
   End Sub
Author's Note: Because this code is in a class and not a Windows form, you cannot use MessageBox.Show unless you have a reference to System.Windows.Forms. And we all know that business objects should not be displaying messages—this is just a sample..

Create a form to allow users to enter person information with TextBoxes to hold the LastName and FirstName entries. Include a button named AddButton to add the person to the list and a button named SaveButton to perform a save operation. If desired, add a grid to display the contents of the list.

Change the declaration section of the form code to read as follows:

   Private PersonList As New List(Of Person)
The code in the Click event for the Add button is as follows:

   Dim newPerson As New Person
   With newPerson
      .LastName = LastNameTextBox.Text
      .FirstName = FirstNameTextBox.Text
   End With
   ' Add the person to the collection
   ' Clear the form for the next entry
   FirstNameTextBox.Text = String.Empty
   LastNameTextBox.Text = String.Empty
This code creates a new Person object, populates it from the TextBox values, and adds it to the list. It then clears the TextBox values for entry of another person's data.

Note: You cannot easily bind the List(Of Person) type to a grid because you need to map individual columns of the grid to specific Person object properties. You can either perform this mapping manually; populating the grid with code, or you can use object binding to bind the grid. Also, if you use object binding, you can automatically assign the properties of the person object to the TextBox values; in this case you could delete lines 2-5 of the above example. See my article, "Object Binding Tips and Tricks" in the March/April 2006 issue of CoDe Magazine for more information.

Repeat the process above for a Task object, creating a Task class with properties such as TaskName and Description and a Save method. Add a form for task information entry with code similar to the data entry form for person data. If you want to save a few steps, you can download the code for this article.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date