J
J.Marsch
All:
I have an interesting problem in front of me. does this sound reasonable,
or ridiculous?
I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color types,
some ints, some strings etc.
I _could_ just use a hashtable of object to contain all of these, but since
they are value types, they would be boxed.
But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}
And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on
//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}
Is this a horrible, horrible idea, or is it preferable to directly storing
the value types in the hashtable (thereby boxing them)?
I have an interesting problem in front of me. does this sound reasonable,
or ridiculous?
I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color types,
some ints, some strings etc.
I _could_ just use a hashtable of object to contain all of these, but since
they are value types, they would be boxed.
But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}
And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on
//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}
Is this a horrible, horrible idea, or is it preferable to directly storing
the value types in the hashtable (thereby boxing them)?