我收到此错误:
可访问性不一致:参数类型'Banjos4Hire.BanjoState'不如方法'Banjos4Hire.Banjo.Banjo(string,int,int,Banjos4Hire.BanjoState)'
使用此代码:
public Banjo(string inDescription, int inPrice, int inBanjoID, BanjoState inState)
{
description = inDescription;
price = inPrice;
banjoID = inBanjoID;
BanjoState state = inState;
}
有谁知道如何解决这个问题?
由于
答案 0 :(得分:1)
如果BanjoState是枚举,我已对其余代码做了一些假设,并添加了注释以显示错误:
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;
}
}
}
<强>提示强>
public Banjo(string description, int price, ...
如果您这样做,您需要更具体地指定您的类字段,因为名称是相同的。你可以通过使用关键字&#39; this&#39;来引用类实例来实现这一点。 this.description = description;
string _description
如果BanjoState是您正在引用的另一个项目中的类,则它与BanjoS在不同的程序集中,并且您无法使用您之外的某些内容创建构造函数范围。
在这种情况下,您需要声明&#34; BanjoState&#34;上课要公开。看一下声明,我想你并不是说这个类没有public关键字。您不能使用参数(BanjoState类型的对象)比使用它进行构造的类更难访问,因为那时您将无法创建公共类的实例。
From the MSDN page on classes:
直接在命名空间内声明的类,而不是嵌套的类 在其他课程中,可以是公共的或内部的。课程是 内部默认。
上面页面示例中的代码(但针对您个性化):
class BanjoState //This class is internal, not public!
{
// Methods, properties, fields, events, delegates
// and nested classes go here.
}