Link to home
Start Free TrialLog in
Avatar of madhatter5501
madhatter5501Flag for United States of America

asked on

C# Math in Windows Form

Hello,

I have been trying for a week to get this to work and have tried many different ways and I am sure it is a lot simpler than I am making it.  I was able to do this in VB and it worked good, but I want to do this in C#.  Basically on Form load, fill Textbox1 and Textbox2 with Random numbers between 0 and 15.  I will add a textbox later for user to type answer.  right now I just want to know how to make these two textboxes do a math calculation and return the answer to the label1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

                private void Form1_Load(object sender, EventArgs e)
                         {

                            //On FormLoad:
                            //Create Two New Random Numbers between 0 and 15 
                            //and Automatically Assign them to 
                            //Textbox1 & Textbox2

                        Random r = new Random();
                        Random v = new Random();

                        Double randomnumber1 = r.Next(0, 15);
                        Double randomnumber2 = v.Next(0, 15);

                        Double num1 = Convert.ToDouble(textBox1.Text);
                        Double num2 = Convert.ToDouble(textBox2.Text);

                        }

                public void button1_Click(object sender, EventArgs e)
                        {
                            //On Button1_Click
                            //Run Math Problem and Display the answer
                            //on label1
                        }
                      
        }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of madhatter5501

ASKER

both good answers and work well
Also, if youre using Textbox controls then the User can change the number, so you should add error handling in case User doesnt enter a valid number.
...
try
            {
                Double num1 = Convert.ToDouble(textBox1.Text);
                Double num2 = Convert.ToDouble(textBox2.Text);
                label1.Text = (num1 * num2).ToString();
            }
            catch (FormatException ex)
            {
                MessageBox.Show("Please enter valid numbers");
            }
Exceptions are expensive. Why use try/catch when there is the perfectly reasonable replacement of double.TryParse?

double num1;
double num2;

if (!double.TryParse(textBox1.Text, out num1) || !double.TryParse(textBox2.Text, out num2)
{
    MessageBox.Show("Please enter valid numbers");
}

Open in new window


...not to mention it's less code   ; )
Personally I think the try ...catch would be better.  If the operation was division and the number to divide by was zero the TryParse would not stop that failing.  (Admittedly there ought to be logic to stop possiblities like that being presented in the first place).
...the number to divide by was zero the TryParse would not stop that failing.
Neither would a:

catch (FormatExcepction ex)

Using try/catch, to cover both scenarios (invalid numbers and division by zero), your code now has to become:

try
{
    Double num1 = Convert.ToDouble(textBox1.Text);
    Double num2 = Convert.ToDouble(textBox2.Text);
    label1.Text = (num1 / num2).ToString();
}
catch (FormatException ex)
{
    MessageBox.Show("Please enter valid numbers");
}
catch (DivideByZeroException ex)
{
    MessageBox.Show("Cannot divide by zero.");
}

Open in new window


Besides, division by zero is something that is easily tested for, isn't it? It makes no sense to catch an exception when you have the option of testing the inputs to prevent a known exception. How is the above any better than:

double num1;
doubld num2;

if (!double.TryParse(textBox1.Text, out num1) || !double.TryParse(textBox2.Text, out num 2))
{
    MessageBox.Show("Please enter valid numbers");
}
else if (num2 == 0)
{
    MessageBox.Show("Cannot divide by zero.");
}
else
{
    label1.Text = (num1 / num2).ToString();
}

Open in new window

Avatar of Mez4343
Mez4343

@Kaufmed
 I agree that using !double.TryParse() is more effiecient than allowing an exception to occur when there is an easy way to prevent the exception.

 Since this question was very basic, I just wanted to make sure the Author saw that there was a potential for error.