A
Arne Vajhøj
Ironically, I suspect that in this particular situation, there never
really should have been a type with 85 properties. Instead, it probably
would have made more sense to use a dictionary/indexer/etc. and have the
caller just provide the name as a string, rather than having it baked
into the code. Such an approach will be more flexible as the options for
data evolve, and is likely to be more compatible/convenient with at
least some of the use scenarios that are going to come up (i.e. setting
or retrieving these properties when the tag for the data is already a
string).
It is easy.
But it is not type safe.
And it could become pretty messy.
I'd much rather have:
class Container
{
private Dictionary<string, int> _data =
new Dictionary<string, int>();
public int this[string key]
{
get { return _data[key]; }
set { _data[key] = value; }
}
}
Does that class provide any value over Dictionary said:…used like this:
private Container _container;
void UpdateLabel(Label label, string key)
{
label.Text = _container[key].ToString();
}
That is pretty easy.
But is that method a frequent requirement??
than this:
class Container
{
public int Key1 { get; set; }
public int Key2 { get; set; }
.
.
.
public int Key85 { get; set; }
}
…used like this:
private Container _container;
void UpdateLabel(Label label, string key)
{
switch (key)
{
case "Key1":
label.Text = _container.Key1.ToString();
break;
case "Key2":
label.Text = _container.Key2.ToString();
break;
case "Key3":
label.Text = _container.Key3.ToString();
break;
.
.
.
case "Key85":
label.Text = _container.Key85.ToString();
break;
}
}
Arne