C#カスタムコレクション
7102 ワード
カスタムタイプ
カスタムコレクションメソッド
public class Product
{
public int Id { get; set; } // ID
public string Name { get; set; } //
public string Code { get; set; } //
public string Category { get; set; } //
public decimal Price { get; set; } //
public DateTime ProduceDate { get; set; } //
/// <summary>
/// ToString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("{0}{1}{2}{3}{4}{5}",
this.Id.ToString().PadLeft(2), this.Category.PadLeft(15),
this.Code.PadLeft(7), this.Name.PadLeft(17), this.Price.ToString().PadLeft(8),
this.ProduceDate.ToString("yyyy-M-d").PadLeft(13));
}
public static ProductCollection GetSampleCollection()
{
ProductCollection collection = new ProductCollection(
new Product { Id = 1, Code = "1001", Category = "Red Wine", Name = "Torres Coronas", Price = 285.5m, ProduceDate = DateTime.Parse("1997-12-8") },
new Product { Id = 3, Code = "2001", Category = "White Spirit", Name = "Mao Tai", Price = 1680m, ProduceDate = DateTime.Parse("2001-5-8") },
new Product { Id = 4, Code = "2013", Category = "White Spirit", Name = "Wu Liang Ye", Price = 1260m, ProduceDate = DateTime.Parse("2005-8-1") },
new Product { Id = 8, Code = "3001", Category = "Beer", Name = "TSINGTAO", Price = 6.5m, ProduceDate = DateTime.Parse("2012-4-21") },
new Product { Id = 11, Code = "1003", Category = "Red Wine", Name = "Borie Noaillan", Price = 468m, ProduceDate = DateTime.Parse("1995-7-6") },
new Product { Id = 15, Code = "1007", Category = "Red Wine", Name = "Pinot Noir Rose", Price = 710m, ProduceDate = DateTime.Parse("1988-9-10") },
new Product { Id = 17, Code = "3009", Category = "Beer", Name = "Kingway", Price = 5.5m, ProduceDate = DateTime.Parse("2012-6-13") }
);
return collection;
}
}
カスタムコレクションメソッド
public class ProductCollection
{
public Hashtable table;
public ProductCollection()
{
table = new Hashtable();
}
public ProductCollection(params Product[] array)
{//
table = new Hashtable();
foreach (Product item in array)
{
this.Add(item);
}
}
public ICollection Keys { get { return table.Keys; } }
/// <summary>
/// Key
/// </summary>
/// <param name="index"> </param>
/// <returns></returns>
public string GetKeys(int index)
{
if (index < 0 || index > table.Keys.Count)
throw new Exception(" ");
string selectN = "";
int i = 0;
foreach (string item in table.Keys)
{
if (index == i)
{
selectN = item; break;
}
i++;
}
return selectN;
}
public Product this[int index]
{
get
{
string key = GetKeys(index);
return table[key] as Product;
}
set { string key = GetKeys(index); table[key] = value; }
}
/// <summary>
/// Key
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public string GetKeys(string Name)
{
foreach (string item in table.Keys)
{
if (item==Name)
{
return item;
}
}
throw new Exception(" ");
}
public Product this[string Name]
{
get
{
string selects = GetKeys(Name);
return table[selects] as Product;
}
set
{
string key = GetKeys(Name);
table.Remove(table[key]);
this.Add(value);
}
}
/// <summary>
///
/// </summary>
/// <param name="item"> </param>
public void Add(Product item)
{
foreach (string key in table.Keys)
{
if(key==item.Code)
throw new Exception(" ");
}
table.Add(item.Code,item);
}
/// <summary>
///
/// </summary>
/// <param name="item"> </param>
public bool Remove(Product item)
{
try
{
table.Remove(item.Code);
return true;
}
catch
{
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool Remove(int index)
{
if (index < 0 || index > table.Count)
throw new Exception(" ");
string key = GetKeys(index);
table.Remove(key);
return true;
}
/// <summary>
///
/// </summary>
public void clear()
{
table = new Hashtable();
}
/// <summary>
///
/// </summary>
public int Count { get { return table.Keys.Count; } }
}