I have a problem when summing all of bx3.text to t2.text.
first I split bx3.text with space
private void total()
{
string[] ps = bx3.Text.Split(new string[] {" "}, StringSplitOptions.None );
t2.Text = ps.Select(x => Convert.ToInt32(x)).Sum().ToString();
}
I did try with t2.text = ps[1] and the number showed was correct.
but when i try to sum it all, I got error
“Input string was not in a correct format” on (x =>
Convert.ToInt32(x))
bx3.text is full of user-input number separated by single space ” “
Try this:
Above code will sum all integers in ps array and it will omit all non integer values.
First scenario: if x is integer (x=1)
Second scenario: if x is not integer (x=”aaa”)
If x is not integer function TryParse always set value of myInt to zero (msdn doc).