Skip to main content
added 84 characters in body
Source Link
user33306
user33306

Your event handlers can be greatly simplified since they all have duplicate code. You can use the Tag property to tell what operation to perform. Then simply assign the same method to handle each event. something like this:

public Form1()
{
    InitializeComponent();
    btnAdd.Tag = '+';
    btnSub.Tag = '-';
    btnMultiply.Tag = '*';
    btnDivd.Tag = '/';
}

private void btn_Click(object sender, EventArgs e)
{
    var clickedButton = (Button)sender;
    Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
    ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
    ratNum2 = new Assignment5(txtRat2.Text);
    ratSum = new Assignment5("0/1");
    switch ((char)clickedButton.Tag)
    {
        case '+':
            {
                ratSum = ratNum1 + ratNum2;
                break;
            }
        case '-':
            {
                ratSum = ratNum1 - ratNum2;
                break;
            }
        case '*':
            {
                ratSum = ratNum1 * ratNum2;
                break;
            }
        case '/':
            {
                ratSum = ratNum1 / ratNum2;
                break;
            }
    }
    txtSum.Text = ratSum.ToString();
}

Your event handlers can be greatly simplified since they all have duplicate code. You can use the Tag property to tell what operation to perform. Then simply assign the same method to handle each event. something like this:

btnAdd.Tag = '+';
btnSub.Tag = '-';
btnMultiply.Tag = '*';
btnDivd.Tag = '/';

private void btn_Click(object sender, EventArgs e)
{
    var clickedButton = (Button)sender;
    Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
    ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
    ratNum2 = new Assignment5(txtRat2.Text);
    ratSum = new Assignment5("0/1");
    switch ((char)clickedButton.Tag)
    {
        case '+':
            {
                ratSum = ratNum1 + ratNum2;
                break;
            }
        case '-':
            {
                ratSum = ratNum1 - ratNum2;
                break;
            }
        case '*':
            {
                ratSum = ratNum1 * ratNum2;
                break;
            }
        case '/':
            {
                ratSum = ratNum1 / ratNum2;
                break;
            }
    }
    txtSum.Text = ratSum.ToString();
}

Your event handlers can be greatly simplified since they all have duplicate code. You can use the Tag property to tell what operation to perform. Then simply assign the same method to handle each event. something like this:

public Form1()
{
    InitializeComponent();
    btnAdd.Tag = '+';
    btnSub.Tag = '-';
    btnMultiply.Tag = '*';
    btnDivd.Tag = '/';
}

private void btn_Click(object sender, EventArgs e)
{
    var clickedButton = (Button)sender;
    Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
    ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
    ratNum2 = new Assignment5(txtRat2.Text);
    ratSum = new Assignment5("0/1");
    switch ((char)clickedButton.Tag)
    {
        case '+':
            {
                ratSum = ratNum1 + ratNum2;
                break;
            }
        case '-':
            {
                ratSum = ratNum1 - ratNum2;
                break;
            }
        case '*':
            {
                ratSum = ratNum1 * ratNum2;
                break;
            }
        case '/':
            {
                ratSum = ratNum1 / ratNum2;
                break;
            }
    }
    txtSum.Text = ratSum.ToString();
}
Source Link
user33306
user33306

Your event handlers can be greatly simplified since they all have duplicate code. You can use the Tag property to tell what operation to perform. Then simply assign the same method to handle each event. something like this:

btnAdd.Tag = '+';
btnSub.Tag = '-';
btnMultiply.Tag = '*';
btnDivd.Tag = '/';

private void btn_Click(object sender, EventArgs e)
{
    var clickedButton = (Button)sender;
    Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
    ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
    ratNum2 = new Assignment5(txtRat2.Text);
    ratSum = new Assignment5("0/1");
    switch ((char)clickedButton.Tag)
    {
        case '+':
            {
                ratSum = ratNum1 + ratNum2;
                break;
            }
        case '-':
            {
                ratSum = ratNum1 - ratNum2;
                break;
            }
        case '*':
            {
                ratSum = ratNum1 * ratNum2;
                break;
            }
        case '/':
            {
                ratSum = ratNum1 / ratNum2;
                break;
            }
    }
    txtSum.Text = ratSum.ToString();
}