Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with HashTable holding objects

Posted on 2006-07-12
11
Medium Priority
?
198 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

688 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