Question

I am getting this error:

Inconsistent accessibility: parameter type 'Banjos4Hire.BanjoState' is less accessible than method 'Banjos4Hire.Banjo.Banjo(string, int, int, Banjos4Hire.BanjoState)'

With this code:

public Banjo(string inDescription, int inPrice, int inBanjoID, BanjoState inState)
{
    description = inDescription;
    price = inPrice;
    banjoID = inBanjoID;
    BanjoState state = inState;
}

Does anyone know how I could fix this?

Thanks

Was it helpful?

Solution

If BanjoState is an enum, I have made some assumptions about the rest of your code and added comments to show what is wrong:

namespace BanjoStore
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Banjo!
            var myFirstBanjo = new Banjo("it's my first!", 4, 67, Banjo.BanjoState.Used);
            //Oh no! The above line didn't work because I can't access BanjoState!
            //I can't used the enum to pass in the value because it's private to the 
            //Banjo class. THAT is why the error was visible in the constructor. 
            //Visual Studio knew this would happen!
        }
    }

    class Banjo
    {
        //These are private by default since there isn't a keyword specified 
        //and they're inside a class:
        string description;
        int price;

        //This is also private, with the access typed in front so I don't forget:
        private int banjoID; 

        //This enum is private, but it SHOULD be:
        //public enum BanjoState
        //Even better, it can be internal if only used in this assembly
        enum BanjoState
        {
            Used,
            New
        }

        public Banjo(string inDescription, int inPrice, int inBanjoID,
                     BanjoState inState)
        {
            description = inDescription;
            price = inPrice;
            banjoID = inBanjoID;
            BanjoState state = inState;
        }
    }
}

Tips

  • As I mentioned, you need access to the BanjoState enum. The least access necessary to get the job done is best. In this case, that probably means internal. If you're only familiar with public and private, go ahead and make it public. You need this access so that when you make an instance of the Banjo class, you can actually specify a BanjoState for the last parameter.
  • An int can't take numbers with a decimal. That might be fine for you, but try out the datatype called decimal.
  • Usually people don't add the word 'in' to parameters. That's a style choice, but in the professional world, style choices get important. I would choose words just like the fields your assigning them to: public Banjo(string description, int price, ... If you do this, you'll need to get more specific to specify your class fields since the names are the same. You can do that by referring to the class instance with the keyword 'this'. this.description = description;
  • You might not want the fields like description, price, etc to be private. If you do, people often name them with an underscore in front: string _description

If BanjoState is a class in another project which you are referencing, it's in a different assembly from BanjoS and you can't make a constructor with something that is outside your scope.

In this case you'll need to declare the "BanjoState" class to be public. Look at the declaration, and I think you'll not that the class does not have the public keyword. You can't have the parameter (an object of BanjoState type) less accessible than the class using it for construction because then you wouldn't be able to create an instance of the public class.

From the MSDN page on classes:

Classes that you declare directly within a namespace, not nested within other classes, can be either public or internal. Classes are internal by default.

Code from the example on the page above (but personalized for you):

class BanjoState //This class is internal, not public!
{
    // Methods, properties, fields, events, delegates 
    // and nested classes go here.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top