[英]Error : Inconsistent accessibility: return type is less accessible than method '
[英]Error 1 Inconsistent accessibility: return type is less accessible than method
当我构建时,VS 显示错误。 这是我的代码:
public Composite buildComposite(ComboBox subs, ComboBox bas)
{
int count = 0;
Composite a = new Composite();
if (subs.SelectedItem != null)
{
foreach (Substance d in listSubstance)
{
if (String.Compare(d.notation, subs.Text) == 0)
{
count++;
a.subs = new Substance(d);
break;
}
}
}
if (bas.SelectedItem != null)
{
foreach (Base g in listBase)
{
if (String.Compare(g.notation, bas.Text) == 0)
{
count++;
a.bas = new Base(g);
break;
}
}
}
if (count > 0)
{
a.equilibrium();
a.settypesubs(arrayDefinition);
return a;
}
else
return null;
}
这是我的错误:
错误 1 不一致的可访问性:返回类型“Project_HGHTM9.Composite”的可访问性低于方法“Project_HGHTM9.Form1.buildComposite(System.Windows.Forms.ComboBox, System.Windows.Forms.ComboBox)” c:\\users\\nguyen\\documents\\ Visual Studio 2013\\Projects\\Project_HGHTM9\\Project_HGHTM9\\Form1.cs 172 26 Project_HGHTM9
您的Composite
类不是public
。 您不能从公共方法返回非公共类型。
如果您没有为非嵌套类指定可访问性,则默认使用internal
。 将public
添加到您的Composite
类定义中:
public class Composite
{
...
另外,如果buildComposite
并不需要是public
(这意味着它只能通过形式内部使用),那么你可以做的方法private
或internal
还有:
private Composite buildComposite(ComboBox subs, ComboBox bas)
{
....
您试图从公共方法返回类Composite
的实例,但Composite
不是公共的,因此无法返回,因为任何调用代码都无法了解有关Composite
类的任何信息,因为它看不到它。
公开您的Composite
类。
public class Composite{...}
或者使返回Composite
的方法与您的类具有相同的可见性(可能是私有的):
private Composite buildComposite(ComboBox subs, ComboBox bas)
其中哪一个合适取决于您是否需要从当前程序集外部调用方法(或使用类)。
默认情况下,类通常尽可能地“隐藏”,因此对于类来说是私有的。 在此处阅读有关默认可见性的更多信息
您的自定义类型 Composite 目前比您的方法 buildComposite 更难访问。 对于其他类要查看此公共方法,它们还必须具有对 Composite 类/结构的公共访问权限。
如果Composite
是在像class Composite
这样的无法访问/不可修改的代码中定义的,您可以尝试将buildComposite
设为内部。 就像internal Composite buildComposite(ComboBox subs, ComboBox bas)
。 这样,通过将方法设置为private ,它仍然更容易访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.