Hi,
I have the following code snippet:-
public double ConvertDoubleToString(double iData)
{
double cData = iData;
string str = Convert.ToString(cData);
return str;
}
local variable str is erroring with "Cannot implicity convert type string to double?
I thought by using the Convert.ToString() static method it would of worked,
I have tried doing an explicit cast\conversion but it fails,
I have also tried returning str 'as' double; but again also failed...
Regards,
Loading
Guest UserPosted Apr 15, 2011, 9:17 AM
public string ConvertDoubleToString(double iData)
{
double cData = iData;
string str = Convert.ToString(cData);
return str;
}
This works too:
public string ConvertDoubleToString(double iData)
{
return Convert.ToString(iData);
}
Sam HobbsPosted Apr 16, 2011, 12:21 AM
For what it is worth, I often don't get a chance to check the forums until the afternoon in the western USA which is late in the evening in the UK (where Vulpes is). John is in the eastern USA and Suthish is in India. We all check in when we can. Suthish has also been active for a couple of years. If you have a good question that one of us can help with then it will just be a matter of time before one of us responds.
Posted Apr 15, 2011, 11:32 AM
Suthish NairPosted Apr 15, 2011, 10:26 AM
Vulpes might be sleeping now, will back soon :). Its nice to see everyone wanted Vulpes to Answer the threads.
Ok..
ConvertDoubleToString methods must returns a double value. But, you are returning a string value (return str;).
Because of that its throwing error.
Modify your code to:
namespace ConsoleApplication1
ToString() method raise exception when the object is null, but in your case always have a default value because
double is a value type. So this will not throw errors. And Convert.ToString() return string.Empty in case of null object.
Hope this helps you..
VulpesPosted Apr 15, 2011, 9:58 AM
return str;
because the compiler knows that there's no implicit conversion (or, for that matter, an explicit conversion) from string to double.
You might be getting confused between the return type of the method in its declaration (i.e. double as it currently stands) and the type of the expression following the return keyword i.e string. There needs to be an implicit conversion from the latter to the former, otherwise the compiler will complain.
Posted Apr 15, 2011, 8:43 AM
Vulpes are you around to answer?
Ankit NandekarPosted Apr 15, 2011, 7:21 AM