Hey. I'm creating a second degree equation solver in the windows form application.(In the form ax^2+bx+c= 0) And the basic programming is complete but I'm having problems making it bullet proof, in case the user type in the wrong number and type in a =0.

So basically i have an a seperate function to read the numbers, Like this:

private void readConstants()
        {
            {
                try
                {
                    aConstant = double.Parse(aConstantTextBox.Text);
                    while(aConstant == 0)
                    {
                        MessageBox.Show("A can't be zero, please retype.");
                    }

                }
                catch (FormatException)
                {
                    MessageBox.Show("A has to be a number, please retype.");
                    aConstantTextBox.Clear();
                }
                try
                {
                    bConstant = double.Parse(bConstantTextBox.Text);
                    
                }
                catch (FormatException)
                {
                    MessageBox.Show("B has to be a number, please retype.");
                    bConstantTextBox.Clear();
                }

                try
                {
                    cConstant = double.Parse(bConstantTextBox.Text);

                }
                catch (FormatException)
                {
                    MessageBox.Show("C has to be a number, please retype.");
                    cConstantTextBox.Clear();
                }
               
           
                // This is just an equation I use to determine wheither the 
                // solution is complex or not. 
                R = bConstant * bConstant - 4 * aConstant * cConstant; 

             
              }

Any suggestions to make this more user friendly? At the moment it isn't quite how i want it.

Edited 5 Years Ago by Asotop: n/a

There is many methods to do the checking, one (and most common) is to use TryParse method:

int myNumber = 0;
if(int.TryParse(textBox1.Text, out myNumber))
{
    //this is a number, 
}
else
{
    MessageBox.Show("Sorry, string is not a number.");
    textBox1.Text = string.Empty;
}

That did the trick! Thanks :)

Posting the entire thing, if somebody else wonders.

if (double.TryParse(aConstantTextBox.Text, out aConstant))
            {
                if (aConstant == 0)
                {
                    MessageBox.Show("A can't be 0. Please retype");
                    aConstantTextBox.Clear();
                }
                if (double.TryParse(bConstantTextBox.Text, out bConstant))
                {
                    if (double.TryParse(cConstantTextBox.Text, out cConstant))
                    {
                    }
                    else
                    {
                        MessageBox.Show("C has to be a number. Please retype");
                        cConstantTextBox.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("B has to be a number. Please retype");
                    bConstantTextBox.Clear();
                }
            }
            else
            {
                MessageBox.Show("A has to be a number. Please retype.");
                aConstantTextBox.Clear();
            }

I am not sure you need to nest all of those if statements together. I mean, it will definately do what you want, it's just hard to look at from another programmers perspective.

string errString = "";
if (double.TryParse(aConstantTextBox.Text, out aConstant))
{
     if (aConstant == 0)
     {
          errString += "A Cannot be 0" + Environment.Newline;
          aConstantTextBox.Clear();
     }
}
else
{
          errString += "A is not valid" + Environment.Newline;
          aConstantTextBox.Clear();
}
if (!double.TryParse(vConstantTextBox.Text, out vConstant))
{
          errString += "V is not valid" + Environment.Newline;
          vConstantTextBox.Clear();
}
if (!double.TryParse(bConstantTextBox.Text, out bConstant))
{
     errString += "B is not valid" + Environment.Newline;     
     bConstantTextBox.Clear();
}
if (!double.TryParse(cConstantTextBox.Text, out cConstant))
{
     errString += "C is not valid" + Environment.Newline;
     cConstantTextBox.Clear();
}
if (errString != "")
{
    MessageBox.Show(errString);
    DontDoMath();
}
else
    DoMath();

Edited 5 Years Ago by skatamatic: n/a

This question has already been answered. Start a new discussion instead.