-
March 15th, 2012, 02:55 PM
#1
Convert to float from textbox
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 ?
-
March 16th, 2012, 12:01 PM
#2
Re: Convert to float from textbox
Originally Posted by Pavaka
Hallo i try to convert the value from the textbox to float i use this code :
Code:
float a;
if(rb1->Checked){a=Convert::ToFloat(tb1->Text);tb26->Text=Convert::ToString(a);}
what have i done wrong ?
Well, putting all that code on one line is not a good idea for starters
But to answer your question properly, you need to give us more details as to what problem you are having.
-
March 16th, 2012, 12:15 PM
#3
Re: Convert to float from textbox
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
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
March 16th, 2012, 02:18 PM
#4
Re: Convert to float from textbox
You could simply use atof()
"Effective teaching is the essence of leadership..."
"There is no substitute for a carefully thought-out design."
If you have found this post to be useful, please Rate it.
-
March 17th, 2012, 04:41 AM
#5
Re: Convert to float from textbox
Originally Posted by mlgoff
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:
Code:
#include <stdlib.h>
// ...
String^ str = textBox1->Text;
char* szValue =
(char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
float fValue = (float)atof(szValue);
Much easier and correct is to use the "managed way", like Eri already suggested:
Code:
float fValue = Convert::ToSingle(textBox1->Text);
So...
When in Rome, do as the Romans do!
In other words, when in managed C++/CLI, do as .NET programmers 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 ]
Last edited by ovidiucucu; March 17th, 2012 at 04:43 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|