Net-informations.com
SiteMap  | About    

ASP.NET HashTable

Hashtable represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key . Both keys and values are Objects.

Add a pair of values in a HashTable

vb.net
  Dim ht As New Hashtable
  ht.Add("1", "Sunday")

ht.Add("2", "Monday")

C#
  Hashtable ht = new Hashtable();
  ht.Add("1", "Sunday");

ht.Add("2", "Monday");

Check if a specified key exist or not

vb.net

  ht.Contains("1")

C#

  ht.Contains("1");

The above code return true if item exist else false.

Remove the specified Key and corresponding Value . HashTable.Remove(Key)

vb.net

  ht.Remove("1")

C#

   ht.Remove("1");

Following code display all key value pairs.

vb.net
  For Each item In ht
    Label1.Text = item.Key & "  --  " & item.Value

Next

C#
  foreach (DictionaryEntry item in ht)
  {
    Label1.Text = item.Key + "   -   " + item.Value;

}

The following ASP.NET program add seven days in a week to a HashTable and bind it to a ListBox control.

Default.aspx

  

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br />
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
Default.aspx.cs
Default.aspx.vb





net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.