Connecting Tech Pros Worldwide Help | Site Map

Hash Table do not contain anything..

dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#1: Oct 1 '09
Hi.. I wrote following code.

Expand|Select|Wrap|Line Numbers
  1. iindex = 0;
  2. foreach (XmlNode Labelnodes1 in Labelnode)
  3. {
  4. countlab = Labelnodes1.Attributes.Count;
  5. for (int i = 0; i < countlab; i++)
  6. {
  7.  
  8. String temp2 = Labelnodes1.Attributes[i].Name;
  9. String temp1 = Labelnodes1.Attributes[i].InnerText;
  10. Proplabval.Add(temp2, temp1);
  11. }
  12. Proptablab.Add(iindex, Proplabval);
  13. Proplabval.Clear();
  14. iindex++;
  15. }
  16.  
I am basically trying to add hash table in hash table.
My for loop(line no 5) will create a hash table.
And it is added to a hash table in for each loop(line no 2)....

Both string temp2,temp1 has correct values..... But when i come out of for each loop my hash table Proptablab do not contain anything....

When i put a break point at line no 12... For all entries it shows the same values.. ie value 1 is same as value2 and equal to value3...
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 699
#2: Oct 1 '09

re: Hash Table do not contain anything..


You are clearing "Proplabval" line no 13. Debug the code and see if the attributes are added properly ...
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#3: Oct 1 '09

re: Hash Table do not contain anything..


I took the Hash tables as class members.. When i put them in my member function. The value is persisting when i come out of the code.....

Regards
Dheeraj Joshi
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#4: Oct 1 '09

re: Hash Table do not contain anything..


You are right... Even i have to comment the clear part......

But another observation.. When it was class member(Hash table)and clear was commented... it was giving same problem.. When i bring it in function and commented clear part, it worked fine..

Regards
Dheeraj Joshi
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#5: Oct 1 '09

re: Hash Table do not contain anything..


The clear there is correct... and not correct. That is, the idea is right, but the implementation is wrong.

You've actually got two hash tables there, one storing attribute names and values, and another storing that hash table in another hash table using an incrementing value (iindex) as the key.

I see what you're trying to do... you want to make sure you're only adding the attributes for that particular node into Proptablab so you clear Proplabval each time you go through, but what you may not have realized is that almost everything except simple value types (ie, int, float, double, etc...) is a reference type.

So you're throwing Proplabval into Proptablab, then clearing it right after, so you're going to have the empty hash table, which is what you're seeing. What you need to do is create a new hash table for the inner loop... I'll modify your code.

Expand|Select|Wrap|Line Numbers
  1. // This code would have been wherever you defined these two HashTables
  2. HashTable Proptablab = new HashTable();
  3. //HashTable Proplabval = new HashTable(); // Do this in the loop now
  4. ...
  5. iindex = 0;
  6. foreach (XmlNode Labelnodes1 in Labelnode)
  7. {
  8.   HashTable Proplabval = new HashTable();
  9.   countlab = Labelnodes1.Attributes.Count;
  10.   for (int i = 0; i < countlab; i++)
  11.   {
  12.     String temp2 = Labelnodes1.Attributes[i].Name;
  13.     String temp1 = Labelnodes1.Attributes[i].InnerText;
  14.     Proplabval.Add(temp2, temp1);
  15.   }
  16.  
  17.   Proptablab.Add(iindex, Proplabval);
  18.   //Proplabval.Clear(); //This can actually be removed
  19.   iindex++;
  20. }
Give that a try :)
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#6: Oct 1 '09

re: Hash Table do not contain anything..


Hey GaryTexmo....

I already Got the solution.. I did same thing as you told... It is working fine...

Thanks for the inputs....

You are right, i almost forgot about the references... I was so stupid when i coded it...

Regards
Dheeraj Joshi
Reply