0

I have to create two classes. the base class is Employee and it needs 2 protected fields. A string Name and an int Number. I then have to create a derived class called ProductionWorker with 4 properties. the first two are the 2 inherited properties from the base class. the next two are double PayRate and int ShiftNum. The user enters this data on a form. Once the show button is clicked these 4 data points need to be intialized into the ProductionWorker object as properties. Then using the object I must show this data as a string.

I've read about : base after the derived class's constructor. But I still can't initialize the ProductionWorker because it doesn't take the entire 4 parameters?

namespace Employee_Form
{
    class Employee
    {
        protected string Name { get; set; }
        protected int Number { get; set; }

        public Employee(string name, int number)
        {
            this.Name = name;
            this.Number = number;
        }
    }
}

namespace Employee_Form { class ProductionWorker : Employee { protected static new string Name { get; set; } protected static new int Number { get; set; } protected int ShiftNum { get; set; } protected double PayRate { get; set; }

    public ProductionWorker(int shiftNum, double payRate) : base (Name, Number)
    {
        this.ShiftNum = shiftNum;
        this.PayRate = payRate;
    }

    public string showData()
    {
       
    }
}

}

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ProductionWorker worker = new ProductionWorker(Convert.ToInt32(textBox3.Text),
                                                           Convert.ToDouble(textBox4.Text),
                                                           textBox1.Text,
                                                           textBox2.Text);
        }
    }
}
Randy
  • 103
  • 5
  • _"I still can't initialize the ProductionWorker because it doesn't take the entire 4 parameters?"_ -- only because you didn't provide them. So provide them. See duplicate for all the details about calling base constructors you need. Of course, the other HUGE problem with your code is that you've redeclared the base properties `Name` and `Number`, using `new` to hide them. Don't do that. – Peter Duniho Apr 27 '21 at 05:31

1 Answers1

1

Your child class has to have all 4 parameters in the constructor. IE:

public ProductionWorker(string name, int number, int shiftNum, double payRate) : base (name, number)

The alternative would to have no explicit constructor, and use the initializer's syntax. IE:

ProductionWorker worker = new ProductionWorker(textBox1.Text, textBox2.Text)
{
    ShiftNum = Convert.ToInt32(textBox3.Text),
    PayRate = Convert.ToDouble(textBox4.Text)
};

^ for that to work, you again need to delete the explicit constructor from the child class.

B.O.B.
  • 704
  • 4
  • 10
  • 1
    *Parameters* are declared, *arguments* are supplied - so the first sentence should be "Your child class has to have all 4 parameters in the constructor" – Jon Skeet Aug 30 '23 at 16:09
  • @JonSkeet Thanks. I'm bad about using those interchangeably. Corrected my post. – B.O.B. Aug 30 '23 at 16:10