C# 参数比方法更难访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9726974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parameter is less accessible than method
提问by James Dawson
I'm trying to pass a list from one form class to another. Here's the code:
我试图将一个列表从一个表单类传递到另一个。这是代码:
List<Branch> myArgus = new List<Branch>();
private void btnLogin_Click(object sender, EventArgs e)
{
// Get the selected branch name
string selectedBranch = lbBranches.SelectedItem.ToString();
for (int i = 0; i < myArgus.Count; i++)
{
if (myArgus[i]._branchName == selectedBranch)
{
// Open the BranchOverview form
BranchOverview branchOverview = new BranchOverview(myArgus[i]);
branchOverview.Show();
}
else
{
// Branch doesn't exist for some reason
}
}
}
And then in my BranchOverviewclass:
然后在我的BranchOverview课堂上:
List<Branch> branch = new List<Branch>();
public BranchOverview(List<Branch> myArgus)
{
InitializeComponent();
branch = myArgus;
}
When I run the code, I get this error:
当我运行代码时,我收到此错误:
Inconsistent accessibility: parameter type 'System.Collections.Generic.List<Argus.Branch>' is less accessible than method 'Argus.BranchOverview.BranchOverview(System.Collections.Generic.List<Argus.Branch>)'
采纳答案by MiMo
You have to declare Branchto be public:
您必须声明Branch为公开:
public class Branch {
. . .
}
回答by xandercoded
By default, fields of a class are privateif no access modifieris present ...
默认情况下,类的字段是private如果不access modifier存在...
回答by Mark Byers
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 Branchclass public if you are using it as a parameter in a public method.
Branch如果您将类用作公共方法中的参数,则需要将其设为公开。
public class Branch { .... }
^^^^^^
Alternatively you could change your method to be internalinstead of public.
或者,您可以将方法更改为internal代替public.
internal BranchOverview(List<Branch> myArgus)
^^^^^^^^
回答by Jon
The constructor of BranchOverviewis 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
BranchOverviewis的构造函数public,这意味着它的形参列表中涉及的所有类型也必须是public。很可能您还没有提供 的可访问性规范Branch,即您已经编写了
class Branch { ... }
which means that Branchis internal.
这意味着Branch是internal。
回答by Simon Whittemore
Change:
改变:
List<Branch> myArgus = new List<Branch>();
to be public.
公开。

