26

How would I parse an empty string? int.Parse(Textbox1.text) gives me an error:

Input string was not in a correct format.
System.FormatException: Input string was not in a correct format.

If the text is empty (Textbox1.text = ''), it throws this error. I understand this error but not sure how to correct this.

1
  • 2
    you wouldn't, check if the string is empty before trying to parse it. if(!string.IsNullOrEmpty(Textbox1.Text)). Obviously this will still give you an error if the string is not a number so add some validation to the input Commented Feb 21, 2012 at 4:35

8 Answers 8

42

If you're looking to default to 0 on an empty textbox (and throw an exception on poorly formatted input):

int i = string.IsNullOrEmpty(Textbox1.Text) ? 0 : int.Parse(Textbox1.Text);

If you're looking to default to 0 with any poorly formatted input:

int i;
if (!int.TryParse(Textbox1.Text, out i)) i = 0;
Sign up to request clarification or add additional context in comments.

3 Comments

the second code snippet suggests that TryParse will leave the previous value intact when returning false but it will zero it instead. better write int i; if (!int.TryParse(Textbox1.Text, out i)) i = 0; or someone thinks the following would work int i = -1; int.TryParse(Textbox1.Text, out i);
out i - it will be assigned to 0 if not parsed, so for zero check and assignment are extra.
Personally, I prefer IsNullOrWhitespace for this.
17

Well, what do you want the result to be? If you just want to validate input, use int.TryParse instead:

int result;

if (int.TryParse(Textbox1.Text, out result)) {
    // Valid input, do something with it.
} else {
    // Not a number, do something else with it.
}

Comments

7
if(!String.IsNullOrEmpty(Textbox1.text))
    var number = int.Parse(Textbox1.text);

Or even better:

int number;

int.TryParse(Textbox1.Text, out number);

Comments

5

Try this:

int number;
if (int.TryParse(TextBox1.Text, out number))
{
    //Some action if input string is correct
}

Comments

5

If the input is a number or an empty string this will work. It will return zero if the string is empty or else it will return the actual number.

int.Parse("0"+Textbox1.Text)

Comments

4

You could also use an extension method like this:

public static int? ToNullableInt32(this string s)
{
    int i;
    if (Int32.TryParse(s, out i)) return i;
    return null;
}

Here's the reference: How to parse a string into a nullable int in C# (.NET 3.5)

Comments

1

I had the same problem with int.TryParse() erroring with negative numbers.

Here's what I used and it worked:

int.TryParse("-1",NumberStyles.Number, CultureInfo.InvariantCulture, out var num);

The NumberStyles enum can be OR'd together with '|', so you can pick and choose the format combinations. NumberStyles.Number covers about any number.

1 Comment

I'm wondering what culture writes negative numbers differently!
-2

you can wrap it with simple try/catch...

6 Comments

Terrible way to accomplish this. Always validate the input - don't plan for exceptions.
This is a valid alternative but you should be clear that you only want catch(System.FormatException)
@Umer - what I meant is you don't need to think a lot just wrap the code with it and get on with your life...
@RobinVanPersi agreed, if you can't find a dirty solution, you can't think a faster solution :)
@johnny: Better worded as "exceptions are for exceptional circumstances" or something. Anyway, the point being this: if you can check your input in a straightforward way before feeding it to stuff, do so. It reads better and helps avoid swallowing exceptions you may not have intended to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.