Conversion problem
how can I convert a string from a textbox into a long or an int ??
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Jun 4, 2007, 5:08 AM EDT
Try:
long num64 = System.Convert.ToInt64(textBox1.Text);
or
int num32 = System.Convert.ToInt32(textBox1.Text);
Adace Gavril Marius8 reputationPosted Jun 4, 2007, 5:30 AM EDT
Posted Jun 5, 2007, 9:30 AM EDT
Just be careful because if you are converting a string to an integer and the string just happens to be alphabetic, such as converting "A" to an integer, the program will crash because obviously "A" is not an cannot be converted to a numeric data type. Try this to prevent that:
bool boolIsIntConverted; //Declare true/false check variable
Int16 intTextBoxInteger;
boolIsIntConverted = Int16.TryParse(textBox1.Text, out intTextBoxInteger);
if (!boolIsIntConverted) //If text box input was not converted
{
MessageBox.Show("Please input a number.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
textBox1.SelectAll();
}
else //If it was converted, all is well. Return to class
{
return;
}
Im writing this code while Im at work so I have nothing to test it on so I am not positive it will work off hand without actually testing it, but Im pretty sure it should work, lol. Ill check it when I get home and make sure and Ill add any edits necessary to make it correct.
AlanRank #669 · 3.6K reputationPosted Jun 5, 2007, 11:15 AM EDT
For .NET 1.0 or 1.1, you can achieve something similar by placing the call to the Convert method in a try/catch statement.
Posted Jun 5, 2007, 12:33 PM EDT
Int16 intTextBoxInteger;
try //Try to convert the contents of textBox1 to an integer
{
intTextBoxInteger = Convert.ToInt16(textBox1.Text);
}
catch //If textBox1 cannot be converted, catch it and throw a programmer defined error message
{
MessageBox.Show("Please input a number.", "Help", MessageBoxButtons.OK,
MessageBoxIcon.Information);
textBox1.Focus();
textBox1.SelectAll();
}
I always like to put out examples, I know I like to see examples of the code with comments on how it works, lol.
Jan MontanoRank #1,295 · 1.3K reputationPosted Jun 6, 2007, 2:23 AM EDT
I don't know how to pm you so I'll post my concern here instead. I noticed that your post is formatted in a nice way. Are you using a tool or editor for it? Normally, I would manually add html tags on my post just for it to be readable especially if it's code I'm posting. But if there's a better, and faster way, that would greatly help. Please advise. Thanks.
Posted Jun 6, 2007, 8:08 AM EDT
Hello,
I just manually color and space everything around. If you have Visual Studio and you copy and paste code from there, itll automatically color it all correctly for you but the curly braces and everything will need to be readjusted as it aligns everything to the left.
Jan MontanoRank #1,295 · 1.3K reputationPosted Jun 6, 2007, 5:30 PM EDT