[Mono-list] Object Lookup with a Compound key

Dwivedi , Ajay Kumar AjayKumar.Dwivedi@dresdner-bank.com
Thu, 20 Jun 2002 11:13:49 +0100


Thanks Rafael,
	I am now using a class as a key. The class is TypeMember.cs and is
present in the Xml.Serialization namespace.
The class Impelments Equals and GetHashCode. The GetHashCode is simply
unchecked sum of individual hashcodes of Type and string.

Happy Hacking,
Ajay

> -----Original Message-----
> From: A Rafael D Teixeira [mailto:rafaelteixeirabr@hotmail.com]
> Sent: Thursday, June 20, 2002 7:57 AM
> To: AjayKumar.Dwivedi@dresdner-bank.com
> Cc: mono-list@ximian.com
> Subject: Re: [Mono-list] Object Lookup with a Compound key
> 
> 
> >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
>