C# Error - Overload for method

I took a button in form.
I have two classes emp and bonus.bonus class is inherited from emp class.
If bonus is giving ie in dialog box if we select ‘Yes’ b ie bonus is added to sal and we get ts ie total sal along with eno.
If bonus is not given ie in dialog box if we select ‘No’ we need to get only sal along with eno.

The following pgm is working fine.

class emp
{
private int eno = 101;
protected int sal = 5000;
public void print()
{
MessageBox.Show("eno = " + eno.ToString() + " sal " + sal.ToString());
}
}
class bonus : emp
{
int b, ts;
public void increment (int x)
{
b = x;
print();
ts = sal + b;
MessageBox.Show("ts =" + ts.ToString());
}
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult dr;
dr = MessageBox.Show ("r u giving bonus", "peers", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
bonus obj = new bonus();
obj.increment(3000);
}
else
{
emp x = new emp();
x.print();
}
}
}

But I want to give values as parameters

So I wrote the pgm as


class emp
{
private int eno;
protected int sal;
public emp(int x, int y)
{
eno = x;
sal = y;
}
public void print()
{
MessageBox.Show("eno =" + eno.ToString() + "sal = " + sal.ToString());
}
}
class bonus : emp
{
private int b, ts;
public bonus(int z)
{
b = z;
//}
int ts;
ts = b + sal;
print();
MessageBox.Show("ts = " + ts.ToString());

}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr;
dr = MessageBox.Show("are u giving bonus", "peers", MessageBoxButtons.YesNo, MessageBoxIcon.None);
if (dr == DialogResult.Yes)
{
emp x1 = new emp(101,5000);
bonus obj = new bonus(3000);
x1.print();
}
else
{
emp x = new emp(101,5000);
x.print();
}
}
}

Now Iam getting error as No overload for method 'emp' takes '0' arguments .How to solve this error


Actually my intension was to ask eno,sal and bonus at runtime and display the results accordingly when I press button which I took in form .how to ask them at runtime .
Can u give me that code .

Questions by aarruunnaa   answers by aarruunnaa

Showing Answers 1 - 9 of 9 Answers

you are getting error because when you create a subclass object, it automatically calls up the base class constructor.

You need to provide a default constructor(no argument constructor) for the base class i.e emp class to get rid off that error.


You can take user inputs using text boxes on your form.

  Was this answer useful?  Yes

Hi aarruunnaa,
You try this code, if it is useful for you send me reply otherwise ask me again dont hesitate 
class emp

{

public int eno,sal;

public emp(){}

public emp(int eno,int sal)

{

this.eno=eno;this.sal=sal;

}

public virtual void print()

{

MessageBox.Show("eno = " + eno.ToString() + " sal " + sal.ToString());

}

}

class bonus : emp

{


public int bon,ts;

public bonus(){}

public bonus(int bon,int eno,int sal):base(eno,sal)

{

this.bon=bon;

}

public override void print()

{

base.print();

ts=bon+sal;

MessageBox.Show("ts = " + ts.ToString());

}

}
private void button1_Click(object sender, System.EventArgs e)

{

DialogResult dr;

dr = MessageBox.Show ("r u giving bonus", "peers", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

if (dr == DialogResult.Yes)

{

bonus obj = new bonus(3000,101,5000);

obj.print();

}

else

{

emp x =
new emp(101,5000);

x.print();

}



}
Thanks,
sudhakar

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions