Hallo i try to convert the value from the textbox to float i use this code :
float a;
if(rb1->Checked){a=Convert::ToFloat(tb1->Text);tb26->Text=Convert::ToString(a);}
what have i done wrong ?
Printable View
Hallo i try to convert the value from the textbox to float i use this code :
float a;
if(rb1->Checked){a=Convert::ToFloat(tb1->Text);tb26->Text=Convert::ToString(a);}
what have i done wrong ?
Actually, there is no such function as Convert::ToFloat(). The "official" .NET name of that type is Single, and float is just an alias name for that type in C++/CLI (and, AFAIK, in C#). Consequentially, the function you need to call is Convert::ToSingle().
Also, as already has been pointed out, putting all that code on a single line without a single whitespace is no good idea with regard to coding style, though of course it doesn't keep the code from compiling successfully.
Finally, this is a C++/CLI question, and as such it's in the wrong place here. There's a separate forum section for C++/CLI: http://www.codeguru.com/forum/forumdisplay.php?f=17
You could simply use atof()
Not quite simple.
You can use atof or any other CRT function in managed C++/CLI, but you have to "scratch your had with the foot".
Something like this:
Much easier and correct is to use the "managed way", like Eri already suggested:Code:#include <stdlib.h>
// ...
String^ str = textBox1->Text;
char* szValue =
(char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
float fValue = (float)atof(szValue);
So...Code:float fValue = Convert::ToSingle(textBox1->Text);
In other words, when in managed C++/CLI, do as .NET programmers do. :)Quote:
When in Rome, do as the Romans do!
@Pavaka:
I'm just curious...
Is your IntelliSense tool out of order and is showing Convert::ToFloat method?
Also, have you lost the Internet connection with MSDN Library? ;)
[ Moved thread ]