Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I'm new here. In my code below, I'm getting an "exception" thrown with additional info saying "Input string was not in a correct format".

I understand what is going on (sort of) but I don't know how to resolve it. If there is a number in multiplyBx (a text box) but no numbers in either startBx or endBx (two other text boxes) I get the aforementioned error above. But I thought I fixed this problem using the TryParse in my code. In some tests the TryParse certainly works, preventing the same type of exception being thrown, if you have all the text boxes blank or with letters or with a number in either startBx or endBx and a letter or blank in multiplyBx. It's just when there is a number in multiplyBx and blank or letters in either startBx or endBx I get the error.

Can anyone advise how to resolve the problem please? Many thanks.

// ##Code Below##

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Loops
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int outputvalue = 0;
            bool isNumber = false;

            isNumber = int.TryParse(startBx.Text, out outputvalue);
            isNumber = int.TryParse(endBx.Text, out outputvalue);
            isNumber = int.TryParse(multiplyBx.Text, out outputvalue);

            int answer = 0;
            listBox1.Items.Clear();
                        

            if (!isNumber)
            {
                MessageBox.Show("Please enter numbers in the text boxes.");
            }

            else
            {
                int loopStart = int.Parse(startBx.Text);
                int loopEnd = int.Parse(endBx.Text);
                int multiplyBy = int.Parse(multiplyBx.Text);

                for (int i = loopStart; i <= loopEnd; i++)
                {
                    answer = multiplyBy * i;
                    listBox1.Items.Add(i + " multiplied by " + multiplyBy + " = " + answer.ToString());


                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            startBx.Clear();
            endBx.Clear();
            multiplyBx.Clear();
        }
    }
}
Posted
Updated 22-Jul-13 1:38am
v2

C#
isNumber = int.TryParse(startBx.Text, out outputvalue);
isNumber = int.TryParse(endBx.Text, out outputvalue);
isNumber = int.TryParse(multiplyBx.Text, out outputvalue);


You overwrite the isNumber variable, so the last call counts. If you enter a number in the multiplyBx text box, it'll continue to parse the rest.

Why don't you just use int.TryParse to get the numbers?

C#
int loopStart, loopEnd, multiplyBy;

if (!int.TryParse(startBx.Text, out loopStart) || 
    !int.TryParse(endBx.Text, out loopEnd) || 
    !int.TryParse(multiplyBx.Text, out multiplyBy))
{
    MessageBox.Show("Please enter numbers in the text boxes.");
}
else
{
  // Do whatever you need to do.
}
 
Share this answer
 
Comments
ridoy 22-Jul-13 8:16am    
+5
Since you do not use the value of output value you could easily check on isNumber by using &.
C#
isNumber = int.TryParse(startBx.Text, out outputvalue) & int.TryParse(endBx.Text, out outputvalue) & int.TryParse(multiplyBx.Text, out outputvalue);
 
Share this answer
 
Comments
2001aa 22-Jul-13 7:59am    
Many thanks for both of your quick posts! I implemented the fix by digimanus and that has resolved the issue, thanks. Thanks also to ^Mo^ for your fix.

Just a final question, which fix you both posted is best to use or are both considered good? I'm hoping to get a career in programming in the future and would like to learn code refactoring techniques also. Many thanks.
Herman<T>.Instance 22-Jul-13 8:03am    
They are both equal. You can pick any.
ridoy 22-Jul-13 8:15am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900