Error 1 Inconsistent accessibility: return type is less accessible than method

回眸只為那壹抹淺笑 提交于 2019-12-08 16:15:53

问题


When I'm building, VS show error. This is my code:

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

This is my error:

Error 1 Inconsistent accessibility: return type 'Project_HGHTM9.Composite' is less accessible than method '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


回答1:


Your Composite class is not public. You can't return a non-public type from a public method.

If you don't specify an accessibility for a non-nested class then internal is used by default. Add public to your Composite class definition:

public class Composite
{
    ...

Alternatively, if buildComposite doesn't need to be public (meaning it's only used internally by the form), then you could make the method private as well:

private Composite buildComposite(ComboBox subs, ComboBox bas)
{
    ....



回答2:


you are trying to return an instance of class Composite from a public method, but Composite is not public therefore can't be returned as any calling code cannot know anything about the Composite class as it cannot see it.

Make your Composite class public.

public class Composite{...}

or make your method which is returning your Composite have the same visibility as your class (probably private):

private Composite buildComposite(ComboBox subs, ComboBox bas)

Which of these is appropriate will depend on whether you need to call the method (or use the class) from outside your current assembly.

By default a class is usually as 'hidden' as it can be, so private for classes. Read more about the default visibility here




回答3:


Your custom type, Composite, is currently less accessible than your method buildComposite. For other classes to see this public method, they must also have public access to the Composite class/struct.




回答4:


if Composite was defined in unreachable/unmodifiable code like class Composite, you could try making buildComposite as internal. Like internal Composite buildComposite(ComboBox subs, ComboBox bas). This way it is still more accessible by making the method private.



来源:https://stackoverflow.com/questions/24041113/error-1-inconsistent-accessibility-return-type-is-less-accessible-than-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!