Solved

Help with HashTable holding objects

Posted on 2006-07-12
11
197 Views
Last Modified: 2010-04-23
Hello,

The other day, I posted a question as to how I could create dynamically named instances of a custom class
(http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21900997.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!
0
Comment
Question by:alanteigne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17093177
Hi alanteigne;

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

Fernando
0
 

Author Comment

by:alanteigne
ID: 17093233
Thanks!  
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17093256
Sorry I get the Requested file does not exist.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:alanteigne
ID: 17093272
Bah!  Sorry, try again.  I put it in the wrong site.  It should be there now...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17093337
OK got it now.
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 500 total points
ID: 17093438
You shouldnt use your widget params as shared!!!

In that way they will change with class scope and not with the class instance!
Remove shared and you're a happy man.

You also don't need the "= New Widget" in the recall method.
0
 
LVL 5

Expert Comment

by:nickhoggard
ID: 17093457
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
0
 

Author Comment

by:alanteigne
ID: 17093512
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!!!
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 17093528
Uhm.. I'm not Fernando :D but you're welcome
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17093638
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
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 17093664
I noted that already in my first comment
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

622 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