# [.net] simple textbox and floating point manipulations?!

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I'm trying to write a small application in C# that has a simple form interface for the user to enter some floating point values into a few text boxes (e.g. txb_Kp, etc) and then, as needed, spit them out a serial port. The serial port portion of the application works fine, but I'm having a difficult time getting the data I want sent in the right format. I'm rather new to C#... Below is a snippet where I'm using the Convert method to get the text box value into floating point. In one place it works, and in one place (denoted with **ToDecimal**) it doesn't. Any suggestions? Thanks in advance, Walt ---- private void SendData() { //buf = Convert.ToBase64CharArray(trk_thrust.Value.ToString(),String(trk_thrust.Value.ToString())); if (CurrentDataMode == DataMode.Text) { // Send the user's text straight out the port //comport.Write(byte.trk_thrust.Value(), 0, 3); comport.Write(trk_thrust.Value.ToString()); comport.Write(txb_Kp.Text.ToCharArray(),0,3); comport.Write(Convert.ToDecimal(txb_Ki.Text),0,sizeof(Convert.**ToDecimal**(txb_Ki.Text)); comport.Write(txb_Kd.Text); comport.Write(txtSendData.Text); Log(LogMsgType.Outgoing, "\n\nUpdating Parameters:\n"); Log(LogMsgType.Outgoing, trk_thrust.Value.ToString() + "\n"); Log(LogMsgType.Outgoing, txb_Kp.Text + "\n"); Log(LogMsgType.Outgoing, txb_Ki.Text + "\n"); Log(LogMsgType.Outgoing, txb_Kd.Text + "\n"); Log(LogMsgType.Outgoing, trk_thrust.Value.ToString() + "\n"); // Show in the terminal window the user's text Log(LogMsgType.Outgoing, txtSendData.Text + "\n"); } else { ....

##### Share on other sites
You're doing something really strange with sizeof.
Try decimal.Parse() and/or decimal.TryParse.

##### Share on other sites
Quote:
 Original post by MagosYou're doing something really strange with sizeof.Try decimal.Parse() and/or decimal.TryParse.

Indeed, in C# you don't use sizeof to get the length of something. The best best way would be to use Decimal.TryParse() as Magos said. To get the length of the data you would then convert it to a string and then get the length.

Eg.
Decimal result = 0;if (Decimal.TryParse(txb_Ki.Text, out result)){	int resultLen = result.ToString().Length;		comport.Write(result, 0, resultLen);}

##### Share on other sites
This solution looks right on - Thank you!

I have something relitivly close to what you wrote. I'm proficent in C but just starting C#. Although I've increased my abilities in C# an order of magnitude since I wrote that original post, I still have a lot of learning to do...

Thanks again for the help.

Walt

1. 1
Rutin
25
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631754
• Total Posts
3002104
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!