Check hashtable keys by ignoring the case

  • Thread starter Thread starter Srikanth
  • Start date Start date
S

Srikanth

Hi

Can any one say how to check a hashtable by ignoring the case of key
supplied. I want the following requirement

It should not allow the user to do the following
Hashtable table = new Hashtable();
a) table.Add("one",1);
b) table.Add("One",1);

by default it executes the above two statements with out any
problem. But my intention is to throw an exception when the table contains
the key by ignoring the case. Is there any way to implement like above?


Thanks in advance

Srikanth
 
Srikanth,

What you want to do is call the constructor that takes an IComparer,
passing a CaseInsensitiveComparer instance. Then, when you call Add with
the same key, it will throw an exception.

It should be noted that particular constructor is marked as Obsolete in
..NET 2.0, in favor of a new interface (only available in 2.0).

Hope this helps.
 
You must convert keys to upper case (or lower case) before storing them...

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
That would require excessive maintinence. You can use a
CaseInsensitiveComparer (passed through the constructor) which would treat
the keys in a case-insensitive manner.
 
The easiest way:
System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable()
 
Back
Top