Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with HashTable holding objects

Posted on 2006-07-12
11
Medium Priority
?
199 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 64

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 64

Expert Comment

by:Fernando Soto
ID: 17093256
Sorry I get the Requested file does not exist.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Expert Comment

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

Accepted Solution

by:
PockyMaster earned 2000 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 64

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview

885 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