Click to See Complete Forum and Search --> : System.IFormatProvider


K7SN
January 3rd, 2006, 07:56 PM
Howdy

I hate using things I don't understand. I wanted a simple boolean function test if a string could be parsed. While I can validate it using the typical Regex("[^.0-9]") I wanted something more helpful for all instances and I need to validate a range. Double.TryParse was just the ticket BUT it has this argument I have no clue what its use is for in my simple situation and wondered what the use is I don't understand. My solution works as I want it to but my variable Huh bothers me. Any advice?



private void btnOK_Click(object sender, System.EventArgs e)
{
bool isError = false;
double val;
System.IFormatProvider Huh = null;

if ( txtBoxConfidenceLevel.Text == "")
{
errorProvider1.SetError(txtBoxConfidenceLevel, "Enter a confidence level");
isError = true;
}
else errorProvider1.SetError(txtBoxConfidenceLevel, "");

if (Double.TryParse(txtBoxConfidenceLevel.Text, System.Globalization.NumberStyles.Float, Huh, out val))
{
if (val <= 0.5)
{
errorProvider1.SetError(txtBoxConfidenceLevel, "Confidence level must be greater than 0.50");
isError = true;
}
if (val >= 1.0)
{
errorProvider1.SetError(txtBoxConfidenceLevel, "Confidence level must be less than 1.00");
isError = true;
}
}
else {
errorProvider1.SetError(txtBoxConfidenceLevel, "Unable to parse entry as a double value");
isError = true;
}
if (!isError)
{
UpdateUserSelection();
this.Close();
}
}

Igor Soukhov
January 4th, 2006, 01:11 AM
Here's the my advice:

Pass the following object CultureInfo.InvariantCulture.NumberFormat instead of passing a null value.

K7SN
January 4th, 2006, 03:54 PM
Thanks Igor.

I like that better than the null but still wonder why for a floating point number they at least didin't overload this method.

Igor Soukhov
January 4th, 2006, 05:51 PM
Thanks Igor.

I like that better than the null but still wonder why for a floating point number they at least didin't overload this method.

I guess they've (MS Architects) done that to encourage developers to learn and understand that they're (developers) really doing.