<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestGenericCollections" Debug="true" %>
Test Generic Collections
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 : Collection 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 customers = new EntityCollection();
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 products = new EntityCollection();
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;
}
}