I’m dealing with a class written by a third party that implements System.Collections.CollectionBase:
public class Palette : CollectionBase, ICloneable
The only rub I have with it is that I can only access its elements via integer index: [0], [1], [2]. I need to enhance this class’ functionality so that I can access an element via a string so I can do something like this:
["asian"] = Color.Yellow, ["black"] = Color.Black, ["White"] = Color.White
So I attempted to wrap this up in my own class. So far I have:
public class NamedPalette : Palette
{
private Dictionary<string, PaletteEntry> paletteEntries =
new Dictionary<string, PaletteEntry>();
public PaletteEntry this[string key]
{
get { return paletteEntries[key]; }
set { paletteEntries.Add(key, value); }
}
public NamedPalette()
{
}
}
public class PaletteEntry
{
private Color color;
private Color color2;
public PaletteEntry(Color color, Color color2)
{
this.color = color;
this.color2 = color2;
}
}
Am I on the right track here? Not sure what to do next.
You’re on the right track.
Just change your set accessor to check for existing entries by replacing your line
set { paletteEntries.Add(key, value); }withset {palletteEntries[key] = value;}All you then need to start doing is to add PalletteEntries to your NamedPallette and using them, such as