InputBox: A simple user input dialog





0/5 (0 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]
}
}