Monday, 28 April 2014

C# Dictionary

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;
namespace Test_N.G
{
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Author A1 = new Author(TextBox1.Text, TextBox2.Text, "AAA", "123-4567");
Author A2 = new Author("TTT", "B", "BBB", "123-7823");
Author A3 = new Author("MS", "C", "CCC", "123-6383");
Dictionary dict = new Dictionary();
dict.Add(A1.Id, A1);
dict.Add(A2.Id, A2);
dict.Add(A3.Id, A3);
lboxDictionary.DataSource = dict;
lboxDictionary.DataBind();
ReadDic(dict);
}
protected void ReadDic(Dictionary dict)
{
//Read Dictionary
string strToEmail = string.Empty;
HashSet setUniqEmail = new HashSet();
foreach (KeyValuePair kv in dict)
{
Author au = kv.Value;
strToEmail += au.AuthorName + ",";
}
string[] sArray = strToEmail.Split(',');
HashSet set = new HashSet(sArray);
string[] result = new string[set.Count];
set.CopyTo(result);
//setUniqEmail = set;
foreach (string i in set)
{
string strBody = string.Empty;
foreach (KeyValuePair kv in dict)
{
Author au = kv.Value;
if ((i != null) & (i != ""))
{
if (i == au.AuthorName)
{
strBody += au.Id + "-" + au.ItemURL + "-" + au.AuthorName + "-" + au.searchKeyWord + System.Environment.NewLine;
}
}
}
Response.Write(strBody);
}
}
}
public abstract class AAbstractEntity
{
private string _id;
public AAbstractEntity(string id)
{
_id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract bool IsValid
{
get;
}
}
public class Author : AAbstractEntity
{
private string _ItemURL;
private string _AuthorName;
private string _searchKeyWord;
public Author(string id, string ItemURL, string AuthorName, string searchKeyWord)
: base(id)
{
_ItemURL = ItemURL;
_AuthorName = AuthorName;
_searchKeyWord = searchKeyWord;
}
public string ItemURL
{
get { return _ItemURL; }
set { _ItemURL = value; }
}
public string AuthorName
{
get { return _AuthorName; }
set { _AuthorName = value; }
}
public string searchKeyWord
{
get { return _searchKeyWord; }
set { _searchKeyWord = value; }
}
public override bool IsValid
{
get
{
if (Id.Length > 0 && AuthorName.Length > 0)
return true;
else
return false;
}
}
public override string ToString()
{
return Id + "," + AuthorName + ", " + ItemURL + "," + searchKeyWord;
}
}
}