protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ResetList();
}
}
void ResetList()
{
HashTableSample.table.Clear();
HashTableSample.table.Add(5123, "Jay");
HashTableSample.table.Add(2569, "Brad");
HashTableSample.table.Add(1254, "Brian");
HashTableSample.table.Add(6839, "Seth");
HashTableSample.table.Add(3948, "Rajesh");
HashTableSample.table.Add(1930, "Lakshan");
HashTableSample.table.Add(9341, "Kristian");
HashTableSample.table.Add(7921, "Stephen");
HashTableSample.table.Add(2839, "Tom");
HashTableSample.table.Add(1829, "Kit");
MakeList();
}
public void MakeList()
{
lstNames.Items.Clear();
foreach (Object o in HashTableSample.table.Keys)
lstNames.Items.Add(o.ToString() + ": " + HashTableSample.table[o]);
}
class HashTableSample
{
public static Hashtable table = new Hashtable();
public static String FindEntry(Object inKey, Object value)
{
StringWriter strWriter = new StringWriter();
Console.SetOut(strWriter);
Console.WriteLine();
if (inKey == null || inKey == "")
inKey = 0;
Int32 key;
try
{
key = Int32.Parse(inKey.ToString());
}
catch (Exception)
{
key = 0;
}
if (value != null)
{
if (table.ContainsValue(value))
{
Console.WriteLine("Yes, Value '{0}' was found in the list!", value);
}
else
{
Console.WriteLine("Sorry, Employee '{0}' could not be found.", value);
}
}
else
{
if (table.Contains(key))
{
Console.WriteLine("Yes, ID '{0}' was found in the list!", key);
}
else
{
Console.WriteLine("Sorry, Employee ID '{0}' could not be found.", key);
}
}
Console.WriteLine();
Console.WriteLine("Now we will print out the entire list of employees.");
Console.WriteLine("ID\tName");
Console.WriteLine("----\t-----------");
foreach (DictionaryEntry d in table)
{
Console.WriteLine("{0}\t{1}", d.Key, d.Value);
}
return strWriter.ToString();
}
public static bool AddEntry(String key, String value)
{
try
{
Int32 i = Convert.ToInt32(key);
table.Add(i, value);
return true;
}
catch (Exception)
{
return false;
}
}
}
public void btnID_Click(Object source, EventArgs e)
{
output.Text = "<PRE>" + HashTableSample.FindEntry(empID.Text, null) + "</PRE>";
btnClear.Visible = true;
}
public void btnName_Click(Object source, EventArgs e)
{
output.Text = "<PRE>" + HashTableSample.FindEntry(0, empName.Text) + "</PRE>";
btnClear.Visible = true;
}
public void btnClear_Click(Object source, EventArgs e)
{
output.Text = "";
empName.Text = "";
empID.Text = "";
btnClear.Visible = false;
}
public void btnReset_Click(Object source, EventArgs e)
{
ResetList();
}
public void btnAdd_Click(Object source, EventArgs e)
{
if (txtAddKey.Text == "" || txtAddVal.Text == "")
{
output.Text = "ADD FAILED: Please ensure you enter both a key, and value entry";
btnClear.Visible = true;
}
else
{
if (HashTableSample.AddEntry(txtAddKey.Text, txtAddVal.Text))
{
MakeList();
btnClear_Click(null, null);
}
else
{
output.Text = "ADD FAILED: Please ensure the following:<br>" +
"1) The key is a valid integer<br>" +
"2) The key does not already exist in the list";
btnClear.Visible = true;
}
}
}
}