mostlylucid

scott galloway's personal blog...
posts - 911, comments - 723, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Fairly interesting technique

So here’s the problem ,you want to store items in a Dictionary *but* you want to be able to access those items using different types of key (so in the example below, I use Guids, strings and longs)…here’s how I usually do this…as usual any comments or suggestions for better ways are appreciated…

using System;

using System.Collections;

 

namespace Configuration

{

    /// <summary>

    /// Summary description for ContentPageDictionary.

    /// </summary>

    public class ContentPageDictionary : DictionaryBase

    {

        private Hashtable cpKeys = new Hashtable();

        private Hashtable cpLongKeys = new Hashtable();

        public new void Clear()

        {

            this.Dictionary.Clear();

            cpKeys.Clear();

            cpLongKeys.Clear();

        }

        public  ContentPage this[string key]

        {

            get

            {

                return (ContentPage)base.Dictionary[key];

            }

            set

            {

                Dictionary.Add(key, value);

                cpKeys.Add(value.Guid, key);

                cpLongKeys.Add(value.ObjectId, key);

            }

        }

 

        public ContentPage  this[long key]

            {

        get

            {

            if(cpLongKeys.Contains(key))

            return  this[cpLongKeys[key] as string];

            return null;

        }

    }

        public ContentPage this[Guid key]

        {

            get

            {

                if(cpKeys.Contains(key))

                    return  this[cpKeys[key] as string];

                return null;

            }

        }

            public ICollection Keys

        {

            get { return (Dictionary.Keys); }

        }

 

        public ICollection Values

        {

            get { return (Dictionary.Values); }

        }

 

        public void Add(string key, ContentPage value)

        {

            Dictionary.Add(key, value);

        }

 

        public bool Contains(string key)

        {

            return (Dictionary.Contains(key));

        }

 

        public void Remove(string key)

        {

            Dictionary.Remove(key);

        }

 

        protected override void OnInsert(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (value.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "value");

        }

 

        protected override void OnRemove(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

        }

 

        protected override void OnSet(Object key, Object oldValue, Object newValue)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (oldValue.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "oldValue");

            if (newValue.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "newValue");

        }

 

        protected override void OnValidate(Object key, Object value)

        {

            if (key.GetType() != typeof (string))

                throw new ArgumentException("key must be of type string.", "key");

 

            if (value.GetType() != typeof (ContentPage))

                throw new ArgumentException("value must be of type ContentPage.", "value");

        }

 

    }

}

Print | posted on Tuesday, March 08, 2005 10:39 AM | Filed Under [ .NET Code Snippets ]

Feedback

Gravatar

# re: Fairly interesting technique

This is where .NET really needs help. In Python I can just store any old thing in a list, and it's all built-in to the language. You shouldn't have to do this much programming for what amounts to plumbing code (I'm not knocking your code, rather just saying it should be a LOT easier than this!).
3/8/2005 1:42 PM | Darrell
Gravatar

# re: Fairly interesting technique

Actually to be fair it is easier than this....you can ditch all of the 'On' handlers from this code...
3/8/2005 1:47 PM | Scott Galloway
Comments have been closed on this topic.

Powered by: