user3000043
user3000043

Reputation:

using TryParse as a bool

I am trying to create a bool that checks to see if a textbox has a number in it, so I have

bool ifParsed = int.TryParse(Txtbox1.Text);

I know you are meant to have an out value, but I don't want to assign it to anything, I just want it to give a true or false value to use in an if statement.

Upvotes: 1

Views: 119

Answers (2)

Darren Young
Darren Young

Reputation: 11100

You could easily add an extension method such as:

public static bool IsInteger(this string value)
{
   int i;
   return int.TryParse(value, out i);
}

And then use like:

if (Txtbox1.Text.IsInteger())
{
   // DO stuff.
}

Your extension method will need to be declared in a static class also.

Upvotes: 3

David Pilkington
David Pilkington

Reputation: 13618

Just use

int i;
bool ifParsed = int.TryParse(Txtbox1.Text, out i);

and then never use i

Upvotes: 3

Related Questions