Link to home
Start Free TrialLog in
Avatar of alanteigne
alanteigne

asked on

Help with HashTable holding objects

Hello,

The other day, I posted a question as to how I could create dynamically named instances of a custom class
(https://www.experts-exchange.com/questions/21900997/Dynamically-Assign-Instance-Name.html)
and was given instructions on how to do so with a "HashTable".  I implemented this in my project, but no matter what I do, it does not appear to store multiple instances of my class.  

I created a barebones project to populate and recall from a HashTable, but have the same problem.  No matter how many items I "Add" to the Hashtable, it appears that while the key's are being added, the objects are not.  The keys all point to the same instance.

Here is the barebones project if anyone could open it up (VS .NET 2003) and see where I'm going wrong:

http://it.createc.com/hashtable.zip (32k)

It's a very basic project only containing functionality to demonstrate my problem, but if you have questions on it, please fire away!
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi alanteigne;

Let me take a look at what you are doing. Will gat back to you.

Fernando
Avatar of alanteigne
alanteigne

ASKER

Thanks!  
Sorry I get the Requested file does not exist.
Bah!  Sorry, try again.  I put it in the wrong site.  It should be there now...
OK got it now.
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I've just had a quick look at your code.

There are a couple of things you might need to look at.

1) The variables in Widget are declared as shared.  Shared means that there is only one value for the variable across all instances of Widget (eg every Widget has the same value for param1).  The fix for that is to remove the Shared keyword from the variable declarations.

2) The hashtable can only contain each key once.  When I ran your program it let me add the first value, but it crashed the second time if I had not changed the key.  You'll need to check if the key exists before adding the item to the hashtable.  I can't remember if .NET 1.1 has a property for ContainsKey on the hashtable, but if it does thats the best thing to use.

Hope this helps

Cheers

Nick
Fernando-

Man, that was it.  I can't believe something so simple was causing the problem!  I mistook that "Shared" flag to mean anything with in the class FILE could use it, not any instance of the class!  

Once again, THANKS!!!
Uhm.. I'm not Fernando :D but you're welcome
By the way you are creating an object of Widget needlessly. When you access the hash table it give you and object which is already a hash table. See statement below.

    Dim myWidget As Widget ' = New Widget

The Shared keyword is that all class created of that yype use the one and only variable.

Fernando
I noted that already in my first comment