Reputation: 3751
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?
Upvotes: 2
Views: 6480
Reputation: 926
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.....
Upvotes: 2
Reputation: 101604
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*/);
Upvotes: 6
Reputation: 6180
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);
Upvotes: 1