Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Implementing a Suitable Data Source

Many articles and books demonstrate how to connect to traditional data sources, such as a table in a database. What may not be obvious is that any object that implements the IEnumerable or IListSource interface can also be used as a data source. The benefit is that you can bind custom objects directly to web pages. For example, you could bind an array of contacts directly to a web page. There is no requirement that you have to use a DataSet.

Since you won't have much trouble finding code that demonstrates binding to a DataSet, we'll take a different approach by binding to a class that doesn't contain a DataSet or DataTable. The two classes we'll define are a Contact class that contains fields describing contact information and a Contacts class that contains an ArrayList of Contact objects.

The Contact class contains First Name, Last Name, Email, and URL properties. The Contacts class contains an ArrayList property. The ArrayList class implements the IEnumerable interface and will consequently let us bind to the DataList web control. Listing 1 shows the Contact and Contacts classes:

Listing 1—Contact Information Classes

Option Explicit On
Option Strict On

Public Class Contact
 Private FFirstName As String
 Private FLastName As String
 Private FEMail As String
 Private FURL As String

 Public Property FirstName() As String
  Get
   Return FFirstName
  End Get
  Set(ByVal Value As String)
   FFirstName = Value
  End Set
 End Property

 Public Property LastName() As String
  Get
   Return FLastName
  End Get
  Set(ByVal Value As String)
   FLastName = Value
  End Set
 End Property

 Public Property EMail() As String
  Get
   Return FEMail
  End Get
  Set(ByVal Value As String)
   FEMail = Value
  End Set
 End Property

 Public Property URL() As String
  Get
   Return FURL
  End Get
  Set(ByVal Value As String)
   FURL = Value
  End Set
 End Property

 Public ReadOnly Property FullName() As String
  Get
   Return String.Format("{0}, {1}", _
    FLastName, FFirstName)
  End Get
 End Property

End Class


Public Class Contacts
 Private FItems As ArrayList

 Public ReadOnly Property Items() As ArrayList
  Get
   Return FItems
  End Get
 End Property

 Public Sub New()
  FItems = New ArrayList()
 End Sub

End Class

The two classes are self-explanatory, so I won't go into detail. The ArrayList in the Contacts class, as mentioned, implements the IEnumerable interface. The IEnumerable interface has one method, GetEnumerator. GetEnumerator returns an instance of an object that implements the IEnumerator interface. The IEnumerator interface has three members: MoveNext, Reset, and Current. Enumerators, an example of the iterator pattern, supports iterating over all of the elements in a collection. This general ability of Enumerators allows instances of classes such as DataTable and our Contacts class to be bound to the DataList web control.

  • + Share This
  • 🔖 Save To Your Account