Generic collections : Generics « Collections « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestGenericCollections" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Test Generic Collections</title>
</head>
<body>
    <form id="form1" runat="server">
   <div id="container">
      <h1>Test Generic Collections</h1>
      This is a tester for our generic collection class.
      <asp:ListBox ID="lboxCustomers" runat="server" />
      <br />Retrieved Customer via index: 
      <em><asp:Label ID="labCustomer" runat="server" /></em>
      <br />
      <asp:CheckBox ID="chkValidCustomer" runat="server" Text="Is Valid?" />    
      
      <asp:ListBox ID="lboxProducts" runat="server" />
      <br />Retrieved Product via index: 
      <em><asp:Label ID="labProduct" runat="server" /></em>
      <br />
      <asp:CheckBox ID="chkValidProducer" runat="server" Text="Is Valid?" /> 
   </div>
    </form>
</body>
</html>


File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.Collections.ObjectModel;



public class Product: AbstractEntity 
{

   private string _name;
   private double _price;

   public Product(string id, string name, double price) : base(id)
  {
      _name = name;
      _price = price;
  }
   public string Name
   {
      get { return _name; }
      set { _name = value; }
   }
   public double Price
   {
      get { return _price; }
      set { _price = value; }
   }

   public override string ToString()
   {
      return Id + "," + Name + "," + Price;
   }

   public override bool IsValid
   {
      get {
         if (Id.Length > 0 && Name.Length > 0)
            return true;
         else
            return false;
      }
   }
}

public class EntityCollection<T> : Collection<T> where T : AbstractEntity
{   

   public T FindById(string id)
   {
      foreach (T entity in this.Items)
      {
         if (entity.Id == id)
            return entity;
      }
      return null;
   }


   public bool IsValid()
   {
      bool valid = true;
      foreach (T entity in this.Items)
      {
         if (!entity.IsValid)
            valid = false;
      }
      return valid;
   }



}

public partial class TestGenericCollections : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Customer c1 = new Customer("1", "A", "H", "123-4567");
       Customer c2 = new Customer("2", "B", "G", "456-1267");
       Customer c3 = new Customer("3", "C", "F", "564-7823");
       Customer c4 = new Customer("4", "D", "E", "253-6383");

       EntityCollection<Customer> customers = new EntityCollection<Customer>();
       customers.Add(c1);
       customers.Add(c2);
       customers.Add(c3);
       customers.Add(c4);

       lboxCustomers.DataSource = customers;
       lboxCustomers.DataBind();

       labCustomer.Text = customers[2].Name;
       chkValidCustomer.Checked = customers.IsValid();

       Product p1 = new Product("1", "ProductOne", 24.00);
       Product p2 = new Product("2", "", 59.00);

       EntityCollection<Product> products = new EntityCollection<Product>();
       products.Add(p1);
       products.Add(p2);

       lboxProducts.DataSource = products;
       lboxProducts.DataBind();

       labProduct.Text = products[0].Name;
       chkValidProducer.Checked = products.IsValid();
    }

}

public abstract class AbstractEntity
{

   private string _id;

  public AbstractEntity(string id)
  {
      _id = id;
  }


   public string Id
   {
      get { return _id; }
      set { _id = value; }
   }


   public abstract bool IsValid
   {
      get;
   }
}

public class Customer: AbstractEntity 
{
   private string _firstName;
   private string _lastName;
   private string _phone;

   public Customer(string id, string firstName, string lastName, string phone): base(id)
   {
      _firstName = firstName;
      _lastName = lastName;
      _phone = phone;
   }


   public string FirstName
   {
      get { return _firstName; }
      set { _firstName = value; }
   }
   public string LastName
   {
      get { return _lastName; }
      set { _lastName = value; }
   }
   public string Phone
   {
      get { return _phone; }
      set { _phone = value; }
   }
   public string Name
   {
      get { return LastName + ", " + FirstName; }
   }

   public override bool IsValid
   {
      get
      {
         if (Id.Length > 0 && LastName.Length > 0)
            return true;
         else
            return false;
      }
   }

   public override string ToString()
   {
      return Id + "," + Name + "," + Phone; 
   }
}








7.4.Generics
7.4.1.Generic collections
7.4.2.Creating a list of Person objects using generics (C#)
7.4.3.Creating a list of Person objects using generics (VB)
7.4.4.A generic Stack class (VB)
7.4.5.A generic Stack class (C#)
7.4.6.A generic Stack class using integers (VB)
7.4.7.A generic Stack class using integers (C#)