Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi Friends,
I have a Hashtable and I want to compare the values stored in it.
e.g
C#
Hastable ht= new Hashtable();

and it has collection as

HTML
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...
C#
for(i=0;i<ht.count;i++)
{
   if(ht[i].value==ht[i+1].value)
        flag=true;
   else
       flag=false;
}



Thanks,
Lok..
Posted
Updated 17-Jan-12 22:29pm
v3
Comments
Sergey Alexandrovich Kryukov 18-Jan-12 3:44am    
Why? Do you know what a hashtable is designed for?
--SA

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:

C#
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
 
Share this answer
 
v2
Comments
radioman.lt 18-Jan-12 6:47am    
System.Collections.Generic.SortedDictionary<TKey, TValue> or System.Collections.Generic.SortedDictionary<TKey, TValue>

;}
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.
C#
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;
    }
  }
 
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