Hashtable question

M

Mounil Kadakia

Hi all,

I am not sure if this is the right group to post this question; if not,
please direct me to an appropriate group.

I have 2 hashtables; htb1 and htb2;
I add 2 elements to each;

htb1.Add(string1,valueA);
htb1.Add(string2,value2);

htb2.Add(string3,valueM);
htb2.Add(string4,valueN);

I want to compare the elements in the 2 hashtables.
ie is string 1 = string3 ; if true, then is valueA = valueM

How do i do it? Any help will be greatly appreciated.

TIA,
Mounil.
 
G

Guest

Hi Mounil,
you could iterate through the Keys collection of one of the hashtable
comparing to see if the values associated with the keys are equal.

Something like:

static void Main(string[] args)
{
Hashtable ht1;
Hashtable ht2;

ht1 = new Hashtable();
ht2 = new Hashtable();

string str1, str2,str3,str4;
str1 = "one";
str2 = "two";
str3 = "one";
str4 = "four";

int int1, int2, int3, int4;
int1 = 1;
int2 = 2;
int3 = 1;
int4 = 4;

ht1.Add(str1, int1);
ht1.Add(str2, int2);

ht2.Add(str3, int3);
ht2.Add(str4, int4);

foreach(string strKey in ht1.Keys)
{
if(ht2.ContainsKey(strKey))
{
//do values match
if((int)ht1[strKey] == (int)ht2[strKey])
{
Console.WriteLine("values match");
}
}
}
}

Hope that helps
Mark R Dawson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top