Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi All.

Dictionary<string,string>

How to create dictionary in asp.net and what are the advantages of it.
When should we use it?

Please help me with an example in vb code.

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 15-Jul-11 3:28am    
Tag it properly, change the title: .NET, not ASP.NET
--SA

You would create the dictionary in the code behind - quite frankly whether it's Win Forms or ASP.NET or pick your .NET library type of choice, the library type is immaterial.

It's very useful when you want to store something with a well defined key that you can look up later. A classic example of a dictionary in ASP.NET is the Session object which can store an arbitrary object (known as the Value) using a meaningful Key.

In your example, you'd create the dictionary using
VB
Dim myDict As New Dictionary(Of String, String)
You would add typically add values to it using
VB
If Not myDict.ContainsKey(theKeyToAdd) Then
  myDict.Add(theKeyToAdd, theValue)
End If
Finally, you can retrieve the value using
VB
Dim value As String
If Not myDict.TryGetValue(theKeyToRetrieve, value) Then
  Console.WriteLine("The key could not be found")
Else
  Console.WriteLine(theValue)
End If
 
Share this answer
 
Comments
Sachin__Sharma 14-Jul-11 7:14am    
Thanks for helping. +5
Pete O'Hanlon 14-Jul-11 8:28am    
You're welcome
Try:
Dim dic As New Dictionary(Of String, String)()
dic.Add("Key", "Value")
A string, string dictionary is basically a list, where each entry has a unique name, rather than a numeric index. If you want to set up a list of people, and their email addresses, then it is useful to be able to say
Dim dic As New Dictionary(Of String, String)()
dic.Add("Mike Smith", "Mike.Smith@ADomain.Com")
dic.Add("Joe Smith", "OldJoe@AnotherDomain.Com")
Dim email As String = dic("Joe Smith")
Which you can't do with a List.
 
Share this answer
 
Comments
Sachin__Sharma 14-Jul-11 7:13am    
Thanks for useful description. +5
Learn the use of Dictionary class
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]

Google for specific issues.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900