Solved

Help with HashTable holding objects

Posted on 2006-07-12
11
193 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

20 Experts available now in Live!

Get 1:1 Help Now