Reputation: 663
I need some help with converting. I want to convert a string to a double so I first did like this.
double meterStartvalue = double.Parse(startMeterReading.Text);
And this is fine as long as you put something in the textbox. And that is not good. So I tried to do like this:
double.TryParse(startMeterReading.Text, out meterStartvalue);
When doing the TryParse I get this error:
Argument 2: cannot convert from 'out meterStartvalue' to 'out double'
The best overloaded method match for 'double.TryParse(string, out double)' has some invalid arguments
Also getting error that the meterStartvelue context does not exist because i use this on some places in the code.
Upvotes: 0
Views: 944
Reputation: 98740
Smells like you forget to declare your meterStartValue
variable before using it in your code.
Try like this;
double meterStartValue;
if (double.TryParse(startMeterReading.Text, out meterStartvalue))
{
// Success
}
Since Double.TryParse
returns boolean
you can check if the conversation is succeed or not with an if statement.
Return Value
Type: System.Boolean
true if string was converted successfully; otherwise, false.
Upvotes: 0
Reputation: 1499770
You need to declare meterStartValue
before you call the method, as otherwise the compiler has no idea what you're talking about:
double meterStartValue;
if (double.TryParse(startMeterReading.Text, out meterStartvalue))
{
// Yes, we managed to parse the text.
}
else
{
// Failed to parse it
}
Note that you don't have to give it an initial value; it will be definitely assigned when the method returns though (with a value of 0 if parsing failed).
Unfortunately there's no way of declaring the variable and calling the method (using the variable as an argument) in a single statement.
Upvotes: 9