问题描述
我想创建一个表单,可以在其中编辑类 TagHandler
的字段.因此,我决定将参数传递给表单的构造函数 TagHandler标签
,其中 tag
-是我要编辑的标签.在我的表单中,我有一个 tag
字段,我可以对其进行编辑,然后获取其数据.例如,在我的主窗体中,我有一个带有 MouseDoubleClick
方法
I want to create a form where I can edit a field of my class TagHandler
. So I decided to pass as a paramter to form's constructor TagHandler tag
where tag
- is a tag I want to edit. In my form I've got a field tag
which I edit and then get its data.For example in my main form I've got a listbox with MouseDoubleClick
method
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = listBox1.SelectedIndex;
TagHandler tg = listData[index];
EditTag edit = new EditTag(tg);
if (edit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
listData[index] = edit.Tag as TagHandler;
}
}
其中 EditTag
是表单
public partial class EditTag : Form
{
public TagHandler tag { set; get; }
public EditTag(TagHandler tag)
{
InitializeComponent();
this.CenterToParent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.tag = tag;
this.label2.Text = tag.Tag;
}
private void button1_Click(object sender, EventArgs e)
{
tag.Data = richTextBox1.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
但是我有这样的错误
可访问性不一致:属性类型'XmlMissionEditor.TagHandler'的访问权限比属性'XmlMissionEditor.EditTag.tag'
可访问性不一致:参数类型'XmlMissionEditor.TagHandler'的访问权限比方法'XmlMissionEditor.EditTag.EditTag(XmlMissionEditor.TagHandler)'
出什么问题了?我什至将 tag
字段设置为 public
,但它仍然显示相同的错误.我的课程 TagHandler
看起来像这样
What's the problem? I even set tag
field as public
but it still shows the same error.My class TagHandler
looks like this
[Serializable]
class TagHandler
{
private string data;
private string tag;
private Color color;
private List<AttributeHandler> attributes;
public TagHandler(string tag, bool close)
{
attributes = new List<AttributeHandler>();
if (close)
{
string s = "/" + tag;
this.tag = s;
}
else
{
this.tag = tag;
}
}
public string Tag
{
get { return tag; }
set { tag = value; }
}
public string Data
{
get { return data; }
set { data = value; }
}
...other methods
}
推荐答案
这些是问题所在:
public TagHandler tag { set; get; }
public EditTag(TagHandler tag)
后者是公共类中的公共方法.因此,它的所有参数及其返回类型也应该是公共的-否则,您说的是可以调用此函数,但不知道调用它的类型"(或者返回的是什么,如果返回的话)类型不是公共的.同样,属性的类型也必须是公共的.
The latter is a public method in a public class. Therefore all its parameters and its return type should be public too - otherwise you're saying "You can call this, but you can't know about the type you're calling it with" (or what it's returning, if it's the return type that's not public. Likewise the type of the property has to be public.
或者将构造函数和属性设置为内部,或者将 TagHandler
类型设置为公共.
Either make the constructor and property internal or make the TagHandler
type public.
这篇关于可访问性不一致:与"EditTag.tag"字段相比,"TagHandler"字段类型的访问性较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!