Monday, January 12, 2009

Hashtable and DictionaryEntry

HashTable Class of System.Collections namespace represents a collection of key/value pairs that are organized based on the hash code of the key.


using System.Collections;

Hashtable ht = new Hashtable();
private void Form1_Load(object sender, EventArgs e)
{
	ht.Add("Am", "Ambily");
	ht.Add("Di", "Divya");
	foreach (DictionaryEntry de in ht)
	{
		listBox1.Items.Add(de.Key);
		listBox2.Items.Add(de.Value);
	}
}

private void button1_Click(object sender, EventArgs e)
{
	ht.Add(textBox1.Text, textBox2.Text);
	ShowHashTable();
}

private void ShowHashTable()
{
	listBox1.Items.Clear();
	listBox2.Items.Clear();
	foreach (DictionaryEntry de in ht)
	{
		listBox1.Items.Add(de.Key);
		listBox2.Items.Add(de.Value);
	}
}

VB .Net







Dim ht As New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	ht.Add("Am", "Ambily")
	ht.Add("Di", "Divya")
	ShowHT()
End Sub
Private Sub ShowHT()
	ListBox1.Items.Clear()
	ListBox2.Items.Clear()
	For Each de As DictionaryEntry In ht
		ListBox1.Items.Add(de.Key)
		ListBox2.Items.Add(de.Value)
	Next
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
	ht.Add(txtKey.Text, txtValue.Text)
	ShowHT()
End Sub
Related Blog ReadOnlyCollectionBase Class Generics and Collection Initializer in .Net

No comments:

Post a Comment