Reputation: 65
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
Upvotes: 1
Views: 1670
Reputation: 1719
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
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;
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.
}
Upvotes: 2