Recipe 9.12 Creating a Hashtable with Max and Min Value Boundaries
Problem
You need to use a
Hashtable in your project that stores only numeric
data between a set maximum and minimum value.
Solution
Create a class whose accessors and methods enforce these boundaries.
This class, MaxMinValueHashtable, allows only
integer values that fall between a maximum and minimum size to be
stored:
using System;
using System.Collections;
[Serializable]
public class MaxMinValueHashtable : Hashtable
{
public MaxMinValueHashtable( ) : base( )
{
}
public MaxMinValueHashtable(int minValue, int maxValue)
: base( )
{
this.minValue = minValue;
this.maxValue = maxValue;
}
protected int minValue = int.MinValue;
protected int maxValue = int.MaxValue;
protected bool readOnly = false;
public bool ReadOnly
{
get {return (readOnly);}
set {readOnly = value;}
}
public override bool IsReadOnly
{
get {return readOnly;}
}
public override object this[object key]
{
get
{
return (base[key]);
}
set
{
if (!readOnly)
{
if (value is int)
{
if ((int)value >= minValue && (int)value <= maxValue)
{
base[key] = value;
}
else
{
throw (new ArgumentOutOfRangeException("value",
value,
"Value must be within the range " + minValue +
" to " + maxValue));
}
}
else
{
base[key] = value;
}
}
else
{
throw (new ArgumentOutOfRangeException("value", value,
"This Hashtable is currently set to read-only."));
}
}
}
// This method has been overridden to allow objects to be
// stored in this Hashtable, as well as integers.
// If you do not wish objects to be stored in this
// Hashtable alongside numeric values, simply throw an
// InvalidOperationException when this method is called.
public override void Add(object key, object value)
{
if (!readOnly)
{
base.Add(key, value);
}
else
{
throw (new ArgumentOutOfRangeException("value", value,
"This Hashtable is currently set to read-only."));
}
}
public void Add(object key, int value)
{
if (!readOnly)
{
if (value >= minValue && value <= maxValue)
{
base.Add(key, value);
}
else
{
throw (new ArgumentOutOfRangeException("value", value,
"Value must be within the range " + minValue +
" to " + maxValue));
}
}
else
{
throw (new ArgumentOutOfRangeException("value", value,
"This Hashtable is currently set to read-only."));
}
}
public override void Remove(object key)
{
if (!readOnly)
{
base.Remove(key);
}
else
{
throw (new ArgumentOutOfRangeException(
"This Hashtable is currently set to read-only."));
}
}
public override void Clear( )
{
if (!readOnly)
{
base.Clear( );
}
else
{
throw (new ArgumentOutOfRangeException(
"This Hashtable is currently set to read-only."));
}
}
}
Discussion
The MaxMinValueHashtable class inherits from
Hashtable and overrides the members that allow
Hashtable values to be added, removed, and
modified. The overloaded constructor for the
MaxMinValueHashtable class is defined here:
public MaxMinValueHashtable(int minValue, int maxValue)
This constructor allows the range of values to be set. Its parameters
are:
minValue
The smallest integer value that can be added as a value in a
key/value pair.
maxValue
The largest integer value that can be added as a value in a key/value
pair.
A public Boolean property called ReadOnly has been
added to this class to allow or prevent the use of the
Add, Remove, and
Clear methods. The IsReadOnly
property of the base Hashtable object cannot be
used in this situation, since the IsReadOnly
property is a read-only property. For the
MaxMinValueHashtable, we needed read/write access
to this property. However, the IsReadOnly property
is overloaded in the MaxMinValueHashtable to
return the value of the ReadOnly property.
The overridden indexer has both get and
set. The get returns the value
that matches the provided key. The
set verifies that this object is not read-only; if
it is not, the value parameter is checked
to determine whether it is an integer. If the
value parameter is an integer, it is
checked to determine whether it is within the boundaries of the
minValue and maxValue fields
before it is set. If the value parameter
is not an integer, it is set using the
key.
There are two Add methods: one takes an object and
the other takes an integer for its value
parameter. The Add method that accepts an object
will add the object to the MaxMinValueHashtable
only when it is not read-only. The other Add
method that accepts an integer for its
value parameter performs the same tests
and adds a new test to determine whether the integer value is
between, or equal to, the minValue and
maxValue fields. If all tests pass, the integer is
added to the MaxMinValueHashtable.
See Also
See the "Hashtable Class" topic in
the MSDN documentation.
|