Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is inaccessible due to its protection level

Tags:

c#

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();    
        }
    }
}
like image 432
dan Avatar asked May 25 '11 13:05

dan


People also ask

Is inaccessible due to its protection level protected?

The error ...is inaccessible due to its protection level relates to a call you're making, which is trying to access a private or protected variable. There are three different types of variable security: public // Anything can access this, change this, and use this, anywhere.

Is inaccessible due to its protection level DLL?

But the error says the protection level is inaccessible. That means the Program class you're trying to create isn't accessible, aka it isn't public. If you're trying to unit test an executable then the executable's Program type isn't going to be accessible by default. Change the accessibility and try again.

How do I fix CS0122?

The error CS0122 is resolved by changing the member's access modifier to public .


3 Answers

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(); 
like image 151
Jamiec Avatar answered Sep 20 '22 22:09

Jamiec


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

like image 26
Matías Fidemraizer Avatar answered Sep 22 '22 22:09

Matías Fidemraizer


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.

like image 28
Bala R Avatar answered Sep 22 '22 22:09

Bala R