Solved

Comparing Hashtables

Posted on 2003-10-23
5
2,472 Views
Last Modified: 2012-05-04
Does anyone know how to compare two hashtables?

Hashtable ht1 = new Hashtable();
ht1.Add( "exampleKey", "exampleValue" );

Hashtable ht2 = new Hashtable();
ht2.Add( "exampleKey", "exampleValue" );

// This returns false
ht1.Equals( ht2 );
0
Comment
Question by:chadboyer
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:vo1d
ID: 9609713
it depends on what you wanna compare.
if you wann check if they are euqal you can check if the dimensions are the same at first.
if the dimensions are the same, you can doublecheck the specific elements to each other.
do you mean that?
0
 

Author Comment

by:chadboyer
ID: 9609873
I want to compare the dimensions and the elements.  I was just hoping that ht1.Equals( ht2 ) would do the work for me.  But it doesn't.
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 125 total points
ID: 9609957


ht1.Equals(ht2) checks to see if the ht1 and ht2 variables refer to the same object.


Hastable ht1 = new Hashtabl();
...
..    Fill Hashtable
.

Hashtable ht2 = ht1;

ht1.Equals(ht2)   //  TRUE


This is because ht1 and ht2 really are the same object


Hastable ht1 = new Hashtabl();
Hastable ht2 = new Hashtabl();
ht1.Equals(ht2) // FALSE

you have two distinct objects here ( two "new" statements means two unique obects)


0
 

Author Comment

by:chadboyer
ID: 9610030
I wrote my own Hashtable compare function.  Thanks anyway...
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9610174


here is some code to compare hashtable values and keys



                  Hashtable ht1 = new Hashtable();

                  ht1.Add("1","one");
                  ht1.Add("2","two");
                  ht1.Add("3","three");
                  ht1.Add("4","four");


                  Hashtable ht2 = new Hashtable();

                  ht2.Add("1","one");
                  ht2.Add("2","two");
                  ht2.Add("3","three");
                  ht2.Add("4","four");

                  IDictionaryEnumerator myEnum = ht1.GetEnumerator();
            
                  bool isSame = true;

                  while ( myEnum.MoveNext() )
                        if (ht2.ContainsKey(myEnum.Key))
                        {
                              if (ht1[myEnum.Key]!=ht2[myEnum.Key])
                              {
                                    isSame = false;
                                    break;
                              }                              
                        }
                        else
                        {
                              isSame = false;
                              break;
                        }

                  Console.WriteLine("Hashtables ht1 & ht2 are {0}the same", (isSame)?"":"NOT ");
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Linq help 1 16
How to compare two table schema using c#? 6 32
How to read XML file attributes... 17 42
asp.net mvc5 3 38
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now