December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to use a generic Dictionary object in your project that stores only numeric data in its value (the key can be of any type)between a set, predefined maximum and minimum value.
Create a class with accessors and methods that enforce these boundaries. The class shown in Recipe 5.9, MaxMinValueDictionary, allows only types to be stored that implement the IComparable interface and fall between a maximum and minimum value.
Example 5-4. Creating a dictionary with max and min value boundaries
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization; [Serializable] public class MaxMinValueDictionary<T, U> where U : IComparable<U> { protected Dictionary<T, U> internalDictionary = null; public MaxMinValueDictionary(U minValue, U maxValue) { this.minValue = minValue; this.maxValue = maxValue; internalDictionary = new Dictionary<T, U>(); } protected U minValue = default(U); protected U maxValue = default(U); public int Count { get { return (internalDictionary.Count); } } public Dictionary<T, U>.KeyCollection Keys { get { return (internalDictionary.Keys); } } public Dictionary<T, U>.ValueCollection Values { get { return (internalDictionary.Values); } } public U this[T key] { get {return (internalDictionary[key]);} set { if (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue) <= 0) { internalDictionary[key] = value; } else { throw (new ArgumentOutOfRangeException("value", ...Read now
Unlock full access