Simple C# Help

This is just a simple question in C#

Page 1 of 1

2 Replies - 504 Views - Last Post: 23 November 2008 - 09:04 PM Rate Topic: -----

#1 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Simple C# Help

Post icon  Posted 23 November 2008 - 08:26 PM

Hello, I would like it if someone could help me with this.
I need to check if an int variable is a string.
These words won't make sense, but this might
int gn = int.Parse(textBox1.Text);


I need to see if the user has entered a string into the textbox rather than an int. Here are my (pitiful) attempts.

if (gn == string)
{
}

if (gn == String)
{
}

if (gn != string)
{
}

if (gn != String)
{
}

/*And I have even tried to use an if checking if the textbox had any letter
 * of the alphabet in it; would have worked except if you used two letters.
 */



Thanks in advance, if anyone helps. :)
~Seth

Is This A Good Question/Topic? 0
  • +

Replies To: Simple C# Help

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Simple C# Help

Posted 23 November 2008 - 08:47 PM

Use the .TryParse() method instead. See http://msdn.microsof...y/f02979c7.aspx
Was This Post Helpful? 1
  • +
  • -

#3 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Re: Simple C# Help

Posted 23 November 2008 - 09:04 PM

View Postn8wxs, on 23 Nov, 2008 - 07:47 PM, said:

Use the .TryParse() method instead. See http://msdn.microsof...y/f02979c7.aspx

Thank you, but I figured it out.
I was parsing the textbox, so I decided to take a blind chance.
I made gn a global variable and went to the textchanged event and used this code

try
{
 gn = int.Parse(textBox1.Text);
}
catch
{
 MessageBox.Show("Error!");
}



Thank you for your help, though :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1