可访问性不一致:与方法C#相比,返回类型的访问性较差 [英] Inconsistent accessibility: return type is less accessible than method C#
问题描述
好的,这真的很奇怪。我有一个私人成员,我想在Form2中使用它。我制作了一个公共静态方法,以便可以将该成员加入Form2。
这是我的代码:
私有静态AppController appController;
专用BreadRepository BreadRep;
私人CakeRepository cakeRep;
私人SandwichRepository sandwichRep;
公共Form1()
{
InitializeComponent();
BreadRep = new BreadRepository();
cakeRep = new CakeRepository();
sandwichRep = new SandwichRepository();
appController = new AppController(breadRep,三明治Rep,cakeRep);
}
公共静态AppController getController()
{
return appController;
}
我试图将Form1中的appController公开,但我什至得到了更多错误。现在,我得到了这个信息: getController()'
任何想法吗?
更新:
这是我的AppController类:
class AppController
{
private面包存储库BreadRep;
私人SandwichRepository sandwichRep;
私人CakeRepository cakeRep;
public AppController(BreadRepository BreadRep,SandwichRepository sandwichRep,CakeRepository cakeRep)
{
this.breadRep = BreadRep;
this.sandwichRep = sandwichRep;
this.cakeRep = cakeRep;
}
public void writeToFile(String file)
{
StreamWriter wr = new StreamWriter(file);
字符串writeMe =;
foreach(在BreadRep.getAll()中面包e)
{
writeMe = writeMe + e.getAll()+ \n;
}
foreach(sandwichRep.getAll()中的三明治e)
{
writeMe = writeMe + e.getAll()+ \n;
}
foreach(cakeRep.getAll()中的Cake e)
{
writeMe = writeMe + e.getAll()+ \n;
}
wr.Write(writeMe);
wr.Close();
}
}
我已将AppController更改为public,但得到了再次,更多的错误。相同的错误,但对于BreadRep,cakeRep,sandwichRep。
问题是,正如@ Selman22解释的那样,您的方法是 public
,而其返回值是 internal
。 (默认情况下,类为内部
。)
如果两个都是公共
或内部
,一切正常。
使类公开$由于对其他类的依赖,c $ c>似乎很困难。而且,它可能不是最好的,因为默认情况下最好使事物不易访问。
使方法内部
从另一端解决了相同的问题。
无论如何,@ Selman22首先是:)。我只加了两美分,所以您也许应该接受他的回答:)。
Ok, so this is really wierd. I have a private member, and I want to use it into Form2. I've made a public static method, so that I can get that member into Form2.
Here is my code:
private static AppController appController;
private BreadRepository breadRep;
private CakeRepository cakeRep;
private SandwichRepository sandwichRep;
public Form1()
{
InitializeComponent();
breadRep = new BreadRepository();
cakeRep = new CakeRepository();
sandwichRep = new SandwichRepository();
appController = new AppController(breadRep , sandwichRep, cakeRep);
}
public static AppController getController()
{
return appController;
}
I've tried to make the appController from Form1 public, but I get even more errors. Right now I get this:
Inconsistent accessibility: return type 'exemplu_map.controller.AppController' is less accessible than method 'exemplu_map.Form1.getController()' Any ideas ?
Update:
Here is my AppController class:
class AppController
{
private BreadRepository breadRep;
private SandwichRepository sandwichRep;
private CakeRepository cakeRep;
public AppController(BreadRepository breadRep, SandwichRepository sandwichRep, CakeRepository cakeRep)
{
this.breadRep = breadRep;
this.sandwichRep = sandwichRep;
this.cakeRep = cakeRep;
}
public void writeToFile(String file)
{
StreamWriter wr = new StreamWriter(file);
String writeMe = "";
foreach(Bread e in breadRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
foreach (Sandwich e in sandwichRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
foreach (Cake e in cakeRep.getAll())
{
writeMe = writeMe + e.getAll() + "\n";
}
wr.Write(writeMe);
wr.Close();
}
}
I've changed AppController to public, but I get again, more errors. The same error, but for breadRep, cakeRep, sandwichRep.
The problem is, as @Selman22 explained, that your method is public
, while its return value is internal
. (Classes are internal
by default.)
If both are public
or internal
, everything should work.
Making class public
appeared to be difficult due to dependencies on other classes. Moreover, it could be not the best, since by default it is better to keep things less accessible.
Making the method internal
solves the same problem from another end.
Anyway, @Selman22 was first :). I just added my two cents, so you should perhaps accept his answer :).
这篇关于可访问性不一致:与方法C#相比,返回类型的访问性较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!