Solved

Help with HashTable holding objects

Posted on 2006-07-12
11
192 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
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 62

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 62

Expert Comment

by:Fernando Soto
ID: 17093256
Sorry I get the Requested file does not exist.
0
 

Author Comment

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

Expert Comment

by:Fernando Soto
ID: 17093337
OK got it now.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 62

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now