A design question

T

tony

Hello!

I have a question about design.

I have below a method called InitData and a struct called Data.
Metod InitData is used for loading a hashtable with some data that is about
number of decimal and some limits for max and min value.
I use a string [] to place key string that is used as key values. When I
want to fetch a post a use a string in this key string array[].
I use the string key value to get the right post from the object [] array
For example if I want to fetch a post for key string O2 I do the following
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "O2" ];
double min = data.MinValue;
double max = data.MaxValue;
int numOfDec = data.NumOfDec;

Now to my question and sometimes a problem is when I spell the key string
incorrect for example if I write
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "o2" ];
here I wrote keystring as o2 insted of O2 which cause runtime error.
I just wonder if you have any better idea how I can solve this in a better
way.

public static Hashtable InitData()
{
Hashtable hashTable = new Hashtable();
string[] str = {"DEFAULT", "T<", "T>", "Tim", "Ar",
"O2", "N2","Temp", "MSlg",
"MStl","Yield","H2O"};

object DEFAULT_Range= new Data(3, 0.0, 100.0);
object TempRange = new Data(0, 1350.0, 1899.0);
object TimeRange = new Data(0, 0.0, 1800);
object GasRange = new Data(0, 0.0, 3000.0);
object MSlgRange = new Data(0, 0.0, 99999.0);
object MStlRange = new Data(0, 0.0, 999999.0);
object YieldRange = new Data(1, 0.0, 200.0);
object H2ORange = new Data(0, 0.0, 200.0);

object[] obj = { DEFAULT_Range, // DEFAULT
TempRange, // T<
TempRange, // T>
TimeRange, // Tim
GasRange, // Ar
GasRange, // O2
GasRange, // N2
TempRange, // Temp
MSlgRange, // MSlg
MStlRange, // MStl
YieldRange, // Yield
H2ORange};

for(int i=0; i < str.Length; i++ )
hashTable.Add(str, obj);

return hashTable;
}

public struct Data
{
private int numOfDec;
private double maxValue, minValue;

public Data(int numberOfDec, double min, double max)
{
numOfDec = numberOfDec;
minValue = min;
maxValue = max;
}

public int NumOfDec
{ get { return numOfDec; } }

public double MaxValue
{ get { return maxValue; } }

public double MinValue
{ get { return minValue; } }
}
 
J

Jon Skeet [C# MVP]

tony said:
I have a question about design.

I have below a method called InitData and a struct called Data.
Metod InitData is used for loading a hashtable with some data that is about
number of decimal and some limits for max and min value.
I use a string [] to place key string that is used as key values. When I
want to fetch a post a use a string in this key string array[].
I use the string key value to get the right post from the object [] array
For example if I want to fetch a post for key string O2 I do the following
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "O2" ];
double min = data.MinValue;
double max = data.MaxValue;
int numOfDec = data.NumOfDec;

Now to my question and sometimes a problem is when I spell the key string
incorrect for example if I write
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "o2" ];
here I wrote keystring as o2 insted of O2 which cause runtime error.
I just wonder if you have any better idea how I can solve this in a better
way.

Well, firstly you could make the strings constants to avoid people
having to use the string literals.

An alternative is to use an enum which would be the key into the map.

Jon
 
P

PS

tony said:
Hello!

I have a question about design.

I have below a method called InitData and a struct called Data.
Metod InitData is used for loading a hashtable with some data that is
about
number of decimal and some limits for max and min value.
I use a string [] to place key string that is used as key values. When I
want to fetch a post a use a string in this key string array[].
I use the string key value to get the right post from the object [] array
For example if I want to fetch a post for key string O2 I do the following
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "O2" ];
double min = data.MinValue;
double max = data.MaxValue;
int numOfDec = data.NumOfDec;

Now to my question and sometimes a problem is when I spell the key string
incorrect for example if I write
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "o2" ];
here I wrote keystring as o2 insted of O2 which cause runtime error.
I just wonder if you have any better idea how I can solve this in a better
way.

I see 2 ways of dealing with this.

1. As Jon pointed out use something like string constants or an enum for the
key. If you are using .Net 2.0 you might consider a Dictionary<TKey, TValue>
instead of a hashtable which you could define as either Dictionary<string,
Data> or Dictionary<MyNewEnumType, Data>. I would prefer the enum because
you are limiting the key to the enum values. You also eliminate the cast.

2. To get similar strongly typed object in 1.1 you can wrap the hashtable in
a class and provide properties like

public Data O2
{
get
{
return (MeltPracLimits.Data)m_dataHashTable[ "O2" ];

}
}

This has 3 advantages. The object returned is already cast. The keys are
located in one place so eliminating typos. You don't deal with the keys
anymore outside of the class.

PS
public static Hashtable InitData()
{
Hashtable hashTable = new Hashtable();
string[] str = {"DEFAULT", "T<", "T>", "Tim", "Ar",
"O2", "N2","Temp", "MSlg",
"MStl","Yield","H2O"};

object DEFAULT_Range= new Data(3, 0.0, 100.0);
object TempRange = new Data(0, 1350.0, 1899.0);
object TimeRange = new Data(0, 0.0, 1800);
object GasRange = new Data(0, 0.0, 3000.0);
object MSlgRange = new Data(0, 0.0, 99999.0);
object MStlRange = new Data(0, 0.0, 999999.0);
object YieldRange = new Data(1, 0.0, 200.0);
object H2ORange = new Data(0, 0.0, 200.0);

object[] obj = { DEFAULT_Range, // DEFAULT
TempRange, // T<
TempRange, // T>
TimeRange, // Tim
GasRange, // Ar
GasRange, // O2
GasRange, // N2
TempRange, // Temp
MSlgRange, // MSlg
MStlRange, // MStl
YieldRange, // Yield
H2ORange};

for(int i=0; i < str.Length; i++ )
hashTable.Add(str, obj);

return hashTable;
}

public struct Data
{
private int numOfDec;
private double maxValue, minValue;

public Data(int numberOfDec, double min, double max)
{
numOfDec = numberOfDec;
minValue = min;
maxValue = max;
}

public int NumOfDec
{ get { return numOfDec; } }

public double MaxValue
{ get { return maxValue; } }

public double MinValue
{ get { return minValue; } }
}
 
S

Saad Rehmani

If you're just worried about case, you can use a case insensitive comparison
inside your hashtable:

m_dataHashTable = new Hashtable( capacity, CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default );

--
Saad Rehmani / Prodika / Dallas / TX / USA
tony said:
I have a question about design.

I have below a method called InitData and a struct called Data.
Metod InitData is used for loading a hashtable with some data that is
about
number of decimal and some limits for max and min value.
I use a string [] to place key string that is used as key values.
When I
want to fetch a post a use a string in this key string array[].
I use the string key value to get the right post from the object []
array
For example if I want to fetch a post for key string O2 I do the
following
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "O2"
];
double min = data.MinValue;
double max = data.MaxValue;
int numOfDec = data.NumOfDec;
Now to my question and sometimes a problem is when I spell the key
string
incorrect for example if I write
MeltPracLimits.Data data = (MeltPracLimits.Data)m_dataHashTable[ "o2"
];
here I wrote keystring as o2 insted of O2 which cause runtime error.
I just wonder if you have any better idea how I can solve this in a
better
way.
Well, firstly you could make the strings constants to avoid people
having to use the string literals.

An alternative is to use an enum which would be the key into the map.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top