How can I get the double value from a textbox in .NET using C++. Do I have to get the text and do an atof() or is there an easier way?
Yes, you have to get the text and parse it - although you could stick within .NET and use Double.TryParse
instead of atof()
, which lets you detect user error more easily.
Convert.ToDouble
and Double.Parse
would both throw exceptions in the case of failure; atof()
will silently ignore bad input from what I remember. Double.TryParse
just returns true
/false
to indicate success/failure, and uses an out
parameter to pass back the result of the parse. This is usually the right thing to do for user input - you want to be able to detect the failure, but exceptions aren't really appropriate as users entering invalid data is a far from exceptional situation.
In .NET you can use Double.Parse, Double.TryParse, or Convert.ToDouble.
You have to parse the text.
Double value = 0.0;
if (Double.TryParse(textBox.Text, out value))
{
// Value is valid here.
}
else
{
// Handle invalid input here.
}
it gives me an error when I try to use Double.TryParse, what do I need to include for this. sorry usually use linux.