Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
Hi Friends,
I have a Hashtable and I want to compare the values stored in it.
e.g
Hastable ht= new Hashtable();
and it has collection as
 
   
Key                            Value
Dropdown   --                  1000
Dropdown   --                  2000
Dropdwon   --                  3000
 
I want to compare the values in Hashtabale
if they are not same, return false;
 
How can I get this??
 
I mean something like this...
for(i=0;i<ht.count;i++)
{
   if(ht[i].value==ht[i+1].value)
        flag=true;
   else
       flag=false;
}
 

Thanks,
Lok..
Posted 17-Jan-12 22:35pm
Edited 17-Jan-12 23:29pm
v3
Comments
SAKryukov at 18-Jan-12 3:44am
   
Why? Do you know what a hashtable is designed for?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As the generics were introduced as early as in .NET Framework v.2.0, it made all non-generic collection types like Hashtable obsolete. Those types were not marked deprecated in order to avoid breaking compatibility with legacy code, but for new development it makes sense to use only generic types.
 
Instead of Hashtable, you can use the types from System.Collections.Generic. You have a choice from System.Collections.Generic.Dictionary<TKey, TValue>, System.Collections.Generic.SortedDictionary<TKey, TValue> or System.Collections.Generic.SortedDictionary<TKey, TValue>, please see:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319.aspx[^].
 
They have identical interface, the difference is the difference balance between redundancy in memory consumption and performance.
 
What else? Ah, looping… Isn't it easy? They all implement the interface IEnumerable<KeyValuePair<TKey, TValue>> and it means… what? Right, it means that you can use foreach:
 
foreach (KeyValuePair<TKey, TValue> pair in myCollection) {
    //for example:
    TKey key = pair.Key; //use it the way you want
    TValue value = pair.Value; //use it the way you want
}
 
Now, look at my comment to the question. You did not fully describe required comparison. Let's assume you want to check up if all values are unique or not. Now, remember the properties of dictionary. All key are unique, right. So, you can create yes another dictionary and use your values as keys. If you add all values as keys to your dictionary, every value is unique is this set.
 
If you wanted something else (look, you should really look for ambiguities in your questions!) — not a problem: I explained you how to iterate through all members, so you can implement the rest by yourself.
 
—SA
  Permalink  
v2
Comments
radioman.lt at 18-Jan-12 6:47am
   
System.Collections.Generic.SortedDictionary<TKey, TValue> or System.Collections.Generic.SortedDictionary<TKey, TValue>
 
;}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If you need to check if any of the hashtable values are equal then you could use the Values.CopyTo method and then work with the array using the ordinary for loop, e.g.
  int[] a = new int[hashtable.Values.Count];
  hashtable.Values.CopyTo(a,0);
  bool flag = false;
  for (int i = 0; i < a.Length-1; i++)
    for (int j = i + 1; j < a.Length; j++)
    {
      if (a[i] == a[j])
      {
        flag = true;
        break;
      }
    }
 
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 459
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 18 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100