Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In else if part, it showing error i.e,

Cannot implicity convert type int to System.EventArgs
Operator '-' cannot be applied to operands of type System.EventArgs and int.

What I have tried:

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        
        public int a, b, c, d;
        public int e, f, g, h;
        public int option;
        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void add_Click(object sender, EventArgs e)
        {
            option=1;
            a = int.Parse(textBox1.Text);
            textBox1.Clear();
        }

        private void sub_Click(object sender, EventArgs e)
        {
            f = 11;
            option=2;
            b = int.Parse(textBox1.Text);
            textBox1.Clear();
        }

        private void equal_Click(object sender, EventArgs e)
        {
             if (g == 10 && textBox1.Text != "")
            {
                c = int.Parse(textBox1.Text);
                d = c + a;
                textBox1.Text = d.ToString();
            }
            else if (f == 11 && textBox1.Text != "")
            {
                  if (textBox1.Text != "")
                {
                    e = int.Parse(textBox1.Text);
                    h = e - b;
                    textBox1.Text = h.ToString();
                }
            }
           
            }
        }
    }
Posted
Updated 6-May-22 22:51pm

1 solution

Look at your code:
        private void equal_Click(object sender, EventArgs e)
        {
...
                    h = e - b;
...           
        }
e is a parameter to the equal_Click method: so it "hides" the version at class level.

There are a couple of ways round this, but the best solution is to simply stop using single character variable names and use names that are descriptive of what the variable does, or is used for.

That way, you code becomes much easier to read and work on, more self documenting, and more reliable because it's obvious when you type something like this by mistake:
C#
total = total + myMothersBrithday;
but not when it looks like this:
C#
h = h + m;
 
Share this answer
 
Comments
Richard MacCutchan 7-May-22 5:22am    
myMothersBrithday
Is that like Bara Brith day? :))
OriginalGriff 7-May-22 5:29am    
Well, cake is involved in both ... :O
CPallini 7-May-22 9:23am    
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