lee.j.baxter -2 Junior Poster in Training

Hey there folks!

I've created a class, ATHENADictionary which extends Dictionary, as listed below...

/// <summary>
    /// Extends the Dictionary class to provide more functionality.
    /// </summary>
    /// <typeparam name="TKey">the key type.</typeparam>
    /// <typeparam name="TValue">the value type.</typeparam>
    public class ATHENADictionary<TKey,TValue> : Dictionary<TKey,TValue>
    {
        /// <summary>
        /// New indexer method, which returns a default value rather than throwing an exception.
        /// </summary>
        /// <param name="key">the key.</param>
        /// <param name="defaultValue">the default value.</param>
        /// <returns>the retrieved or default value.</returns>
        public TValue this[TKey key, TValue defaultValue]
        {
            get { return this.ContainsKey(key) ? this[key] : defaultValue; }
        }

        /// <summary>
        /// Converts the dictionary into an IEnumerable to allow easy access to values.
        /// </summary>
        /// <param name="dictionary">the dictionary to convert.</param>
        /// <returns>the values.</returns>
        public static implicit operator IEnumerable<TValue>(ATHENADictionary<TKey, TValue> dictionary)
        {
            return dictionary.Values;
        }
    }

I'm now writing an even more advanced class which will allow me to produce a tree-like dictionary, but I've ran into a potential problem...

public class ATHENATreeDictionary<TKey, TValue> : ATHENADictionary<TKey,TValue>
    {
        // Fields.
        private ATHENADictionary<TKey, ATHENATreeDictionary<TKey, TValue>> m_childNodes =
            new ATHENADictionary<TKey, ATHENATreeDictionary<TKey, TValue>>();

        /// <summary>
        /// Accesses the tree in an intuitive way.
        /// </summary>
        /// <param name="defaultValue">the default value to take.</param>
        /// <param name="keys">the keys that lead to the value.</param>
        /// <returns>the value.</returns>
        public TValue this[TValue defaultValue, params TKey[] keys]
        {
        }
    }

I'm confused as to how the last method will behave if I have a ATHENATreeDictionary where the key and value types are the same...

  • If I supplied a default value and no keys then the indexer would conflict with the default Dictionary indexer.
  • If I supplied a default value and one key then the indexer would conflict with the added ATHENADictionary indexer.

So my question is, how would I overcome this? Does my new indexer automatically override the other 2 in the above cases, or would I need to add something into my code to ensure that it does?

Thanks, Lee.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.