2

I have some textboxes in my C# windows form application. I want to do the following:

inRed = Convert.ToInt32(tbRed.Text.ToString().Length < 0 ? tbRed.Text = "0" : tbRed.Text);
inGreen = Convert.ToInt32(tbGreen.Text.ToString().Length < 0 ? tbGreen.Text = "0" : tbGreen.Text);
inBlue = Convert.ToInt32(tbBlue.Text.ToString().Length < 0 ? tbBlue.Text = "0" : tbBlue.Text);

inCyan = Convert.ToInt32(tbCyan.Text.ToString().Length < 0 ? tbCyan.Text = "0" : tbCyan.Text);
inMagenta = Convert.ToInt32(tbMagenta.Text.ToString().Length < 0 ? tbMagenta.Text = "0" : tbMagenta.Text);

If the textbox doesn't have a value, enter a 0 and convert to integer, otherwise convert the value of the textbox to integer.

I am getting the following error for inCyan, where the textbox is empty:

Input string was not in a correct format.

How can I achieve what I am looking for?

6
  • 3
    Try using Length <= 0 instead of just Length < 0 Commented Apr 13, 2015 at 13:49
  • 2
    Why do you add ToString to a string property? Commented Apr 13, 2015 at 13:50
  • @DavidG Wow... was missing the =. Thanks. Commented Apr 13, 2015 at 13:52
  • 1
    For entering numeric values you could use the NumericUpDown control. It eliminates parsing at all. Commented Apr 13, 2015 at 13:52
  • 1
    @juharr In reality I wouldn't use this method anyway as it will still generate exceptions if non numeric values are entered. Commented Apr 13, 2015 at 13:58

3 Answers 3

6

Instead of Convert.ToInt32, use Int32.TryParse. This gives you feedback regarding if it was a valid integer. e.g.

String textboxValue = "1";
Int32 i;
if (!String.IsNullOrWhitespace(textboxValue) && // Not empty
    Int32.TryParse(textboxValue, out i)) { // Valid integer
  // The textbox had a valid integer. i=1
} else {
  // The texbox had a bogus value. i=default(Int32)=0
  // You can also specify a different fallback value here.
}

As a follow-up, String.IsNullOrWhitespace makes it easy to decipher if a value is supplied, but (depending on your .NET version) is may not be available (and you may only have String.IsNullOrEmpty.

If need be, the polyfill is something long the lines of:

Boolean SringIsNullOrWhitespace(String input)
{
    return !String.IsNullOrEmpty(input) && input.Trim().Length > 0;
}

Also, if you find yourself trying to perform this parsing frequently, you could refactor it into a helper class:

public static class ConvertUtil
{
    public Int32 ToInt32(this String value)
    {
        return ToInt32(value, default(Int32));
    }
    public Int32 ToInt32(this String value, Int32 defaultValue)
    {
#if NET4
        if (!String.IsNullOrWhiteSpace(value))
#else
        if (!String.IsNullOrEmpty(value) && value.Trim().Length > 0)
#endif
        {
            Int32 i;
            if (Int32.TryParse(value, out i))
            {
                return i;
            }
        }
        return defaultValue;
    }
}

// explicit
inRed = ConvertUtil.ToInt32(tbRed.Text, 0/* defaultValue*/);
// As extension
inRed = tbRed.Text.ToInt32(0/* defaultValue*/);
Sign up to request clarification or add additional context in comments.

Comments

2

You can do something like

// Initialise variable with 0
int value;

// Try parse it, if it's successful and able to parse then value is set to         the int equivalent of your text input
int.TryParse(inputVariable, out value);

return value

This is a simple way of dealing with your problem - note, if the parse fails then it returns 0 to value.

How you would apply it to your particular problem.

int inMagenta;
int.TryParse(tbMagenta, out inMagenta);

etc.....

1 Comment

you need to use "out" for out parameter
1

You can use tryparse.

int inRed;  //default value will be 0 , if the string is not in a  valid form
Int32.TryParse(tbRed.Text.ToString(), out inRed);

3 Comments

Just adding some information for the OP, TryParse will return the value 0 if the conversion is unsuccessful. More info here on MSDN
No, TryParse returns a bool. Correct way to check would be if(TryParse(...)).
@DionV. yes it gives both the Int output in out parameter and the conversion status in boolean.

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.