Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Using a string as a hastables key

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thechaosengine - 06 Aug 2005 20:37 GMT
Hi all,

Is it ok to use a string as the key for Hashtable entries? I want to use
the name of entity in question, which I know will always be unique.
Do I have to do anything fancy equality-wise or are there any caveats I should

be aware of?

Thanks to anyone who can advise.

Kindest Regards

tce
Bob Grommes - 06 Aug 2005 21:37 GMT
No problem at all.

In System.Collections.Specialized is a StringDictionary class that may be
useful to you in this scenario.  It is a wrapper around Hashtable to enforce
that keys must be strings.

--Bob

> Hi all,
>
[quoted text clipped - 9 lines]
>
> tce
Adam Calderon (MCSD, MCAD, MCP) - 11 Aug 2005 03:22 GMT
If you really want to make sure your code is reliable put some
checking in place before you add the entry.

StringDictionary stringDic = new StringDictionary();
string stringKey = "FirstOne";
stringDic.Add(stringKey,"One");

//comment out below and an error occurs
//stringDic.Add(stringKey,"Two");

//in your code before you add your entry check to see if it exists
if(stringDic.ContainsKey(stringKey) = false)
{
    stringDic.Add(stringKey,"Two");
}

- Adam

>No problem at all.
>
[quoted text clipped - 17 lines]
>>
>> tce
Lloyd Dupont - 07 Aug 2005 01:05 GMT
the potential caveat about keys are the following:
- changing value hence changing Hashcode() or Equals() result

string are immutable therefore excellent candidate

in .NET 2.0 you could also use
Dictionary<string, MyType>

Signature

There are 10 kinds of people in this world. Those who understand binary and
those who don't.

> Hi all,
>
[quoted text clipped - 9 lines]
>
> tce

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.