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

In your base class Clubs the following are declared protected

  • club;
  • distance;
  • cleanclub;
  • scores;
  • par;
  • hole;

which means these can only be accessed by the class itself or any class which derives from Clubs.

In your main code, you try to access these outside of the class itself. eg:

Console.WriteLine("How far to the hole?");
myClub.distance = Console.ReadLine();

You have (somewhat correctly) provided public accessors to these variables. eg:

public string mydistance
{
    get
    {
        return distance;
    }
    set
    {
        distance = value;
    }
}        

which means your main code could be changed to

Console.WriteLine("How far to the hole?");
myClub.mydistance = 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! ;)


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.


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.


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();