473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Hash Table do not contain anything..

Dheeraj Joshi
1,123 Expert 1GB
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...
Oct 1 '09 #1
5 2133
PRR
750 Expert 512MB
You are clearing "Proplabval" line no 13. Debug the code and see if the attributes are added properly ...
Oct 1 '09 #2
Dheeraj Joshi
1,123 Expert 1GB
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
Oct 1 '09 #3
Dheeraj Joshi
1,123 Expert 1GB
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
Oct 1 '09 #4
GaryTexmo
1,501 Expert 1GB
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 :)
Oct 1 '09 #5
Dheeraj Joshi
1,123 Expert 1GB
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
Oct 1 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
44
by: gokkog | last post by:
Hi there, There's a classic hash function to hash strings, where MULT is defined as "31": //from programming pearls unsigned int hash(char *ptr) { unsigned int h = 0; unsigned char *p =...
13
by: ababeel | last post by:
Hi I am using a calloc in a hash table program on an openvms system. The calloc is used to declare the hash table char **pHashes; pHashes = calloc(hash_size,sizeof(char *)); //hash_size = 101 ...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
24
by: Alexander Mahone | last post by:
Hello, I'm looking for an hash function to be used for an hash table that will contain structs of a certain kind. I've looked into Sourceforge.net, but so far I've found only hash functions for...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.