473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

A design question

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[i], obj[i]);

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; } }
}
Sep 20 '06 #1
3 1889
tony wrote:
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

Sep 20 '06 #2
PS

"tony" <jo*****************@telia.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...
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,
Dataor 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[i], obj[i]);

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; } }
}

Sep 20 '06 #3
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 wrote:
>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

Sep 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.