Hashtable with special access of values

  • Thread starter Thread starter hk
  • Start date Start date
H

hk

Hello,

I would like to create a special version of hashtable,
either inherited from hastable or from DictionaryBase.

I would like to be able to access the class from outside
in the following fassion:

spec_htb.Add("key","abc");
spec_htb.Add("key123",123);

spec_htb["key"] returns doubles but fails on the strings
spec_htb.Strings["key"] returns just strings, fails on doubles
spec_htb.Doubles["key"] returns just doubles, fails on strings

Internally the class should save everything to just one table
independent from the type.

Any suggestions how to accomplish this?

Regards,

Holger
 
When you say "fails", do you mean "acts like they're not there"?

I would composition here rather than inheritance: create a new class
that encapsulates two hash tables, one for strings and one for doubles.
Implement your methods to look in one hash table or the other depending
upon what's needed.

That said, there would be some interesting issues to resolve here:

What happens when you say

spec_htb["key"] = 0.25;

and the entry for "key" already exists and is a string? Do you replace
it? Do you allow up to _two_ entries for "key", one a string and one a
double? Or do you throw an exception?

Are there any operations that return mixed hash table entries, some
doubles and some strings? What about enumerators, do they enumerate
over everything in the composite table, or do you have separate
enumerators for doubles and strings?

Anyway, the answers to some of these questions might change the design.
Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top