[Mono-list] Object Lookup with a Compound key

A Rafael D Teixeira rafaelteixeirabr@hotmail.com
Wed, 19 Jun 2002 23:26:34 -0300

>From: "Dwivedi , Ajay Kumar" <AjayKumar.Dwivedi@dresdner-bank.com>
>Hi All,
>	I need to store and retrieve objects with a compound key.
>This is required for the SoapAttributeOverrides class. The class
>stores SoapAttributes object for each Member of a class. So the
>key to be used in the hashtable consists of Type of the class and
>Name of the member.
>	Now I have 3 ways in which this can be implemented.
>1. Make the key as 'Type.FullType+".."+MemberName' , with ".."
>    seperating Type and Member. This assumes that ".." can't occur
>    in the FullName or MemberName. This is the easiest, but I am
>    not sure if this is a language neutral approach.

No names accepts whitespace (at least for languages taht comply with the 
CLR), so you could use a space or a newline to concatenate those two 
strings. But I would not go that route.

>2. Use a jagged 2D hashtable. The main hashtable is indexed by
>    Type and each value contains another hashtable which is
>    indexed by member names. However this approach might require
>    too many hashtables.

I think itīs too expensive...

>3. Use a new class which emcompasses the Type and MemberName and
>    use it as a key. This approach would however require creation
>    of a new key everytime a search is made.

I would try this one, but I would try to hide these details inside a 
tailored subclass of hashtable. Something like:

class SoapAttributesCollection : Hashtable

struct specialKey
  string typeKeyPart;
  string memberKeyPart;

  public specialKey(System.Type typeKeyPart, string memberKeyPart)
    this.typeKeyPart = typeKeyPart.FullTypeName;
    this.memberKeyPart = memberKeyPart;

// Indexer
public SoapAttributes this[
   System.Type typeKeyPart,
   string memberKeyPart]
  get {
    return (SoapAttributes)(this[specialKey(typeKeyPart, memberKeyPart)]);
  set {
    this[specialKey(typeKeyPart, memberKeyPart)] = value;

public void Add(
   System.Type typeKeyPart,
   string memberKeyPart,
   SoapAttributes value )
  Add(new specialKey(typeKeyPart, memberKeyPart), value);

// so on for ContainsKey, Remove... if needed

I would also experiment with tailoring the hashcodeprovider (hcp member) to 
see if I could use System.Type inside the specialKey structure.

Hope it is of some help for you...

Wonderfull Hacking

Rafael Teixeira
Brazilian Developer

Chat with friends online, try MSN Messenger: http://messenger.msn.com