0

I create a new instance of ProductionWorker called worker which itself is a derived class of Employee. Anyway both class's and their properties are protected. According to my HW they have to be. Well I thought using auto properties let me around that. But its still saying I don't have access?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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(textBox1.Text, 
                                                           Convert.ToInt32(textBox2.Text),
                                                           Convert.ToInt32(textBox3.Text),
                                                           Convert.ToDouble(textBox4.Text));
            textBox5.Text = $"Name: {worker.Name}\n Number: {worker.Number}\n Shift Number: {worker.ShiftNum}\n Hourly Pay Rate: {worker.PayRate}";
            /*OnActivated the line above I can't access the worker props and I get a red squiggly'*/
                



        }

        
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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



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

        /*public string showData()
        {
           
        }*/
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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;
        }
    }
}

spabsa
  • 151
  • 1
  • 11
  • You can't access static members via the instance of an object. – SᴇM Apr 27 '21 at 08:33
  • Why are you shadowing the base class Name / Number properties? – Matt Evans Apr 27 '21 at 08:33
  • Summary of the linked question: `protected` means that the class and derived classes of that class (i.e. `Employee` and `ProductionWorker`) can access the member with that access modifier. Outside classes cannot access protected members. You can read about all of the access modifiers in the [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers). – ProgrammingLlama Apr 27 '21 at 08:34
  • By definition [`protected`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected), _A protected member is accessible within its class and by derived class instances._ – Self Apr 27 '21 at 08:38
  • 1
    Ps I also find `protected static new string Name { get; set; }` weird. [`static`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static) is use to _declare a static member, which belongs to the type itself rather than to a specific object_. It means that the name is on the type `ProductionWorker`, and not on one instance. It can be used for a `static int EmployeeCount`, it will be common to all employee. But a name common to all employee is weird – Self Apr 27 '21 at 08:43
  • @Self I'm really confused then, because my homework problem says these fields all must be protected and to use get and set. And then when that button event goes off. the user input must be stored in the object and then displayed from that object. – spabsa Apr 27 '21 at 08:45
  • @Self I was totally unaware static should not be there, I fixed that – spabsa Apr 27 '21 at 08:46
  • Remove `protected string Name { get; set; }` from `ProductionWorker`. It isn't needed. It _already_ has a `Name` property on its base cass. – mjwills Apr 27 '21 at 08:49
  • You can also have different access level on get and set. It may be more logic to have protection on set, so only the class and it's derived can change a name. for example `public string Name { get; private set; }` means only the class can change and set the name, but everyone can ask for the name. – Self Apr 27 '21 at 08:52
  • @mjwills Thanks man I thought that was unnecessary. – spabsa Apr 27 '21 at 08:54
  • @Self I've been reading on it more and saw that you could do that. I'm still confused(I'm a noob btw) because I know all fields must be protected/private and I'm supposed to use get and set. and I'm reading that `public string Name {get; set;}` is actually creating a property, and a hidden backing field as well. You think this is what I'm supposed to do to mark off both criteria? – spabsa Apr 27 '21 at 08:59
  • @Self and is that hidden backing field protected/private? – spabsa Apr 27 '21 at 09:00
  • 1
    Yes, auto property are just syntactic sugar. The backup field is always private. reference : [Auto-Implemented Properties](https://stackoverflow.com/a/6001936/14900199). For getting the mark, I don't know. – Self Apr 27 '21 at 09:27
  • Not that the backing field is annonimous and may not be a readable name. like in this [example](https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZRwAM6AcgIYC2ApvugOYMAuA3OgA4BOASwBuNdkwDOHbgF90hYmkwAWdAFkAFAEp88otOTSgA=). you can see the compilation result in the right window. – Self Apr 27 '21 at 09:39

0 Answers0