不一致的可访问性:返回类型比方法C#

前端之家收集整理的这篇文章主要介绍了不一致的可访问性:返回类型比方法C#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,所以这真的很厉害.我有一个私人会员,我想使用它到Form2.我已经做了一个公共静态方法,所以我可以把该成员加入到Form2中.

这是我的代码

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;
}

我试图使FormController从Form1公开,但我得到更多的错误.现在我得到这个:

Inconsistent accessibility: return type ‘exemplu_map.controller.AppController’ is less accessible than method ‘exemplu_map.Form1.getController()’
Any ideas ?

更新:

这是我的AppController类:

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();
    }
}

我已将AppController更改为public,但是我再次收到更多错误.同样的错误,但对于breadRep,cakeRep,sandwichRep.

解决方法

问题是,@ Selman22解释说,你的方法是public,而它的返回值是内部的. (默认情况下,类是内部的)

如果两者都是公共的或内部的,一切都应该起作用.

由于依赖于其他类,让上课人员看起来很难.此外,它可能不是最好的,因为默认情况下更好的是保持更少的可访问性.

使内部方法从另一端解决相同的问题.

无论如何,@ Selman22是第一个:).我刚刚加了我的两分钱,所以你也许应该接受他的回答:).

猜你在找的C#相关文章