Thursday, May 21, 2009

Hashtables in a Flash

This is purely ASP.NET, but when creating custom MOSS features, this is something I have used more than a few times in order to temporarily store a subset of data coming from Sharepoint lists. Dont forget to add a reference to the System.Collections namespace beforehand.

There's nothing special about this article, just a quick reference for me.

Create a Hashtable
C#:
Hashtable objHash = new Hashtable();

VB:
Dim objHash As New Hashtable()

Add some data in it
C#:
objHash.Add("FirstName", "Asif");
objHash.Add("LastName", "Malik");
objHash.Add("Number", 1);

VB:
Dim objHash As New Hashtable()
objHash.Add("FirstName", "Asif")
objHash.Add("LastName", "Malik")
objHash.Add("Number", 1)

Search for a specific key:
C#
if (objHash.ContainsKey("FirstName"))
{
   Console.WriteLine("Key is present");
}

Loop through the items:
C#:
foreach (DictionaryEntry entry in estformsHashTable)
{
   if (entry.Value.ToString()=="")
   {
      //do something here
   }
}

VB:
Dim objEntry As DictionaryEntry
For Each objEntry In objHash
   'Do something here
Next

No comments:

Post a Comment