Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

InputBox: A simple user input dialog

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
You have introduced a Globalization / Regional problem by requiring the default value to be a string.double? value = InputBox.GetDouble("xyz", "1.0"); //Note the dotdouble? value2 = InputBox.GetDouble("xyz", "1,0"); //Note the commaOne of these two will fail, but not on every machine or...

You have introduced a Globalization / Regional problem by requiring the default value to be a string.

double? value = InputBox.GetDouble("xyz", "1.0"); //Note the dot
double? value2 = InputBox.GetDouble("xyz", "1,0"); //Note the comma

One of these two will fail, but not on every machine or country or even on the the same line. The 'ToString()', 'Parse()', and 'string.Format()' methods are running under the regional settings of the thread. Which is by default the same as the RegionalSettings for the logged on user, but it does not have to be. For more information, read the helpfile for these functions.

We can elevate the pain for the default value by making the default value the same type as the result; this helps greatly, unless string is the native value for the default, in which case you might need two versions of the method.

Simple fix:

public static double? GetDouble(string caption, double?defaultValue)
{
    ClearLastError();
    // The ToString will convert the value to the Culture of the thread.
    // If you run Code Analisys on this line, it will tell you that
    // you should be more explicit with the culture, I'll leave that as homework. ;-)
    using (InputForm inForm = new InputForm(caption, defaultValue.ToString()))
    {
[edit: and the rest]
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kabwla.Phone
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.
 
Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.
 
Either being an appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 25 Dec 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid