Click here to Skip to main content
15,907,225 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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 calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int calc()
        {

            if (radioButton1.Checked)
            {
                int x;
                x = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
                return x;
            }
            else if (radioButton2.Checked)
            {
                int y;
                y = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);
                return y;
            }

            else if (radioButton3.Checked)
            {
                int p;
                p = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);
                return p;
            }

            else if (radioButton4.Checked)
            {
                int q;
                q = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
                return q;
            }



        }

        private void button1_Click(object sender, EventArgs e)
        {

            int z = calc();
            textBox3.Text = Convert.ToString(z);

        }
    }
}
Posted
Comments
[no name] 15-Oct-14 7:29am    
throw an exception in case you don't find the radioButton.

Each method which have return value (not void) must return something. In your case, method "calc()" must return an integer.
You can solve issue by adding one anther
C#
else
statement, or just add
C#
rerturn 0
at the end of method (or return something what makes more sense for you).
Also it will be nice to consider to use switch statement instead of so many else if.
 
Share this answer
 
C#
public int calc()
      {

          if (radioButton1.Checked)
          {
              int x;
              x = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
              return x;
          }
          else if (radioButton2.Checked)
          {
              int y;
              y = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);
              return y;
          }

          else if (radioButton3.Checked)
          {
              int p;
              p = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);
              return p;
          }

          else if (radioButton4.Checked)
          {
              int q;
              q = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
              return q;
          }

    else{

        return 0;

       }


      }

What happens is you have not ended the if elseif. So, if all conditions fail then what the function returns. Please specify and use the above code. This might work.
Hope this helps.
Thanks
:)
 
Share this answer
 
The above answers are correct. You might like rewrite the function in a better way with less variable declarations. Take a look at the below code you can see the difference.

C#
public int calc()
       {
           var result = 0;
           try
           {
               if (radioButton1.Checked)
               {
                   result = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
               }
               else if (radioButton2.Checked)
               {
                   result = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);

               }

               else if (radioButton3.Checked)
               {
                   result = Convert.ToInt32(textBox1.Text)*Convert.ToInt32(textBox2.Text);
               }

               else if (radioButton4.Checked)
               {
                   result = Convert.ToInt32(textBox1.Text)/Convert.ToInt32(textBox2.Text);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
           return result;
       }
 
Share this answer
 
Note that you are using integers only, and the results of any division will be an integer, and so you will lose any fractional result: 5 / 2 => 2

I'd encourage you to change the Type of your arguments/return value to 'double.

The problem with returning an integer when no RadioButton is Checked is that some of your numeric operations may result in the same integer you choose to return if no RadioButton is Checked: how can you know if the #0 that came back from your 'Calc function is the result of RadioButton2 being Checked and #100 is subtracted from #100, or is a result of no RadioButton being Checked ?

To avoid this I suggest you make the return value a nullable Type: [^]
C#
public int? calc()
{
    int? result = null;

    int i1 = Convert.ToInt32(textBox1.Text);
    int i2 = Convert.ToInt32(textBox2.Text);

    if (radioButton1.Checked)
    {
      result = i1 + i2;
    }
    else if (radioButton2.Checked)

    // and so on

    return result;
}
So if you call 'Calc with no RadioButton checked, and the string "100" in both TextBoxes, you can test for the null result, and handle it in whatever way you want to:
C#
radioButton2.Checked = true;

int? test = calc();

if(test != null)
{
    // valid result
}
else
{
    // null result
}
The other thing you should consider is doing error checking if the user enters any incorrect value, or there's no text, inside the TextBoxes. That's called 'Validation, and there are several ways to approach that. Here's one:
C#
int? result = null;

int i1, i2;

if (!(Int32.TryParse(textBox1.Text, out i1) && Int32.TryParse(textBox2.Text, out i2))) return result;

// if you reach here you have valid integers in i1 and i2
You should also test for a divide by zero error.
 
Share this answer
 
v2
Put return 0 or some other non-valid value just before closing the function...if no radion button is checked, it'll be returned.

also, since you're not using the variables for additional checking you can simplify the code by removing
int q
assign q
return q

with simply return (your formula)

If this helps, please take time to accept this solution. Thank you.
 
Share this answer
 
add return 0; just before calc() closing bracket.

Error occurs because no value is returned in case of system doesn't enter in any condition.

OR

remove last if from else.
this should be
C#
else 
          {
              int q;
              q = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
              return q;
          }

instead of

C#
else if (radioButton4.Checked)
          {
              int q;
              q = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
              return q;
          }
 
Share this answer
 
thanks everyone ..it reallly helped me
 
Share this answer
 
AT the end of the if block just add an additional statement return 0;
 
Share this answer
 
C#
public int calc()
        {
            int x=0;

            if (radioButton1.Checked)
            {

                x = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);

            }
            else if (radioButton2.Checked)
            {

                x = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);

            }

            else if (radioButton3.Checked)
            {

                x = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);

            }

            else if (radioButton4.Checked)
            {

                x = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);

            }

            return x;

        }
 
Share this answer
 

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