Parameter is less accessible than method

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

I'm trying to pass a list from one form class to another. Here's the code:

List myArgus = new List();  private void btnLogin_Click(object sender, EventArgs e) {     // Get the selected branch name     string selectedBranch = lbBranches.SelectedItem.ToString();     for (int i = 0; i 

And then in my BranchOverview class:

List branch = new List();  public BranchOverview(List myArgus) {     InitializeComponent();      branch = myArgus; }

When I run the code, I get this error:

Inconsistent accessibility: parameter type 'System.Collections.Generic.List' is less accessible than method 'Argus.BranchOverview.BranchOverview(System.Collections.Generic.List)'

回答1:

You have to declare Branch to be public:

public class Branch {   . . .  }


回答2:

As the error message says, the type of all parameters of a method must be at least as accessible as the method itself.

You need to make your Branch class public if you are using it as a parameter in a public method.

public class Branch { .... }  ^^^^^^

Alternatively you could change your method to be internal instead of public.

internal BranchOverview(List myArgus) ^^^^^^^^


回答3:

The constructor of BranchOverview is public, which means that all types involved in its formal parameter list must also be public. Most probably you have not provided an accessibility specification for Branch, i.e. you have written

class Branch { ... }

which means that Branch is internal.



回答4:

By default, fields of a class are private if no access modifier is present ...



回答5:

Change:

List myArgus = new List();

to be public.



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