[c#] is inaccessible due to its protection level

I can't figure this out. The problem is that the distance, club, cleanclub, hole, scores and par all say inaccessible due to protection level and I don't know why because I thought I did everything right.

namespace homeworkchap8
{
    public class Clubs
    {
        protected string club;
        protected string distance;
        protected string cleanclub;
        protected string scores;
        protected string par;
        protected string hole;            

        public string myclub
        {
            get { return club; }
            set {club = value; }
        }        
        public string mydistance
        {
            get { return distance; }
            set { distance = value; }
        }        
        public string mycleanclub
        {
            get { return cleanclub; }
            set { cleanclub = value; }
        }       
        public string myscore
        {
            get { return scores; }
            set { scores = value; }
        }       
        public string parhole
        {
            get { return par; }
            set { par = value; }
        }       
        public string myhole
        {
            get { return hole; }
            set { hole = value;}
        }
    }   
}

this is the derived class:

namespace homeworkchap8
{
    public class SteelClubs : Clubs, ISwingClub
    {
        public void SwingClub()
        {
            Console.WriteLine("You hit a " + myclub + " " + mydistance);
        }

        public void clean()
        {
            if (mycleanclub != "yes")
            {
                Console.WriteLine("your club is dirty");
            }
            else
            {
                Console.WriteLine("your club is clean");
            }
        }

        public void score()
        {   
            Console.WriteLine("you are on hole " + myhole + " and you scored a " + 
                myscore + " on a par " + parhole);
        }            
    }
}

This is the interface:

namespace homeworkchap8
{
    public interface ISwingClub
    {
        void SwingClub();
        void clean();
        void score();
    }  
}

here is the main code:

namespace homeworkchap8
{
    class main
    {    
        static void Main(string[] args)
        {    
            SteelClubs myClub = new SteelClubs();
            Console.WriteLine("How far to the hole?");
            myClub.distance = Console.ReadLine();
            Console.WriteLine("what club are you going to hit?");
            myClub.club = Console.ReadLine();
            myClub.SwingClub();

            SteelClubs mycleanclub = new SteelClubs();
            Console.WriteLine("\nDid you clean your club after?");
            mycleanclub.cleanclub = Console.ReadLine();
            mycleanclub.clean();

            SteelClubs myScoreonHole = new SteelClubs();
            Console.WriteLine("\nWhat hole are you on?");
            myScoreonHole.hole = Console.ReadLine();
            Console.WriteLine("What did you score on the hole?");
            myScoreonHole.scores = Console.ReadLine();
            Console.WriteLine("What is the par of the hole?");
            myScoreonHole.par = Console.ReadLine();

            myScoreonHole.score();

            Console.ReadKey();    
        }
    }
}

This question is related to c#

The answer is


The reason being you can not access protected member data through the instance of the class.

Reason why it is not allowed is explained in this blog


It's because you cannot access protected member data through its class instance. You should correct your code as follows:

namespace homeworkchap8
{
    class main
    {    
        static void Main(string[] args)
        {    
            SteelClubs myClub = new SteelClubs();
            Console.WriteLine("How far to the hole?");
            myClub.mydistance = Console.ReadLine();
            Console.WriteLine("what club are you going to hit?");
            myClub.myclub = Console.ReadLine();
            myClub.SwingClub();

            SteelClubs mycleanclub = new SteelClubs();
            Console.WriteLine("\nDid you clean your club after?");
            mycleanclub.mycleanclub = Console.ReadLine();
            mycleanclub.clean();

            SteelClubs myScoreonHole = new SteelClubs();
            Console.WriteLine("\nWhat hole are you on?");
            myScoreonHole.myhole = Console.ReadLine();
            Console.WriteLine("What did you score on the hole?");
            myScoreonHole.myscore = Console.ReadLine();
            Console.WriteLine("What is the par of the hole?");
            myScoreonHole.parhole = Console.ReadLine();

            myScoreonHole.score();

            Console.ReadKey();    
        }
    }
}

myClub.distance = Console.ReadLine();

should be

myClub.mydistance = Console.ReadLine(); 

use your public properties that you have defined for others as well instead of the protected field members.


You need to use the public properties from Main, and not try to directly change the internal variables.


You organized class interface such that public members begin with "my". Therefore you must use only those members. Instead of

myScoreonHole.hole = Console.ReadLine();

you should write

myScoreonHole.myhole = Console.ReadLine();

Dan, it's just you're accessing the protected field instead of properties.

See for example this line in your Main(...):

myClub.distance = Console.ReadLine();

myClub.distance is the protected field, while you wanted to set the property mydistance.

I'm just giving you some hint, I'm not going to correct your code, since this is homework! ;)


In your Main method, you're trying to access, for instance, club (which is protected), when you should be accessing myclub which is the public property that you created.