c# - 如何修复不一致的可访问性错误
问题描述
我们正在制作产品发票 - 提供商品编号、描述和价格。我似乎无法让它合作。我已经完成并确保 InvItem 的类是公开的,但它仍然给我相同的错误代码 CS0050
Inconsistent accessibility: return type 'List<InvIte>' is less accessible than method InvItemDB.GetItems()'
我已经检查了代码并确保它与我的教科书相匹配,并且所有课程都是公开的。这只是我的代码的一部分 --- 它不是全部 4 或 5 种形式。如果需要,我也可以显示这些,但这是给出错误消息的那个。
public partial class frmNewItem : Form
{
public frmNewItem()
{
InitializeComponent();
}
private InvItem item = null;
public InvItem GetNewItem()
{
this.ShowDialog();
return item;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (IsValidData())
{
item = new InvItem(txtItemNo.Text, txtDescription.Text, Convert.ToDecimal(txtPrice));
this.Close();
}
}
private bool IsValidData()
{
return Validator.IsPresent(txtItemNo) &&
Validator.IsInt32(txtItemNo) &&
Validator.IsPresent(txtDescription) &&
Validator.IsPresent(txtPrice) &&
Validator.IsDecimal(txtPrice);
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
邀请项目代码:
class InvItem
{
public string itemno;
public string description;
public decimal price;
public InvItem()
{
}
public InvItem(string itemno, string description, decimal price)
{
this.ItemNo = itemno;
this.Description = description;
this.Price = price;
}
public string ItemNo
{
get{return itemno;}
set { itemno = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
public string GetDisplayText()
{
return itemno + ", " + price.ToString("c") + ", " + description;
}
public string GetDisplayText(string sep)
{
return itemno + sep + price.ToString("c") + sep + description;
}
主要形式:
public partial class frmInvMaint : Form
{
public frmInvMaint()
{
InitializeComponent();
}
private List<InvItem> products = null;
private void frmInvMaint_Load(object sender, EventArgs e)
{
products = InvItemDB.GetItems();
FillItemListBox();
}
private void FillItemListBox()
{
lstItems.Items.Clear();
foreach (InvItem p in products)
{
lstItems.Items.Add(p.GetDisplayText("\t"));
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmNewItem newItemForm = new frmNewItem();
InvItem product = newItemForm.GetNewItem();
if (product != null)
{
products.Add(product);
InvItemDB.SaveItems(products);
FillItemListBox();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i = lstItems.SelectedIndex;
if (i != -1)
{
InvItem product = (InvItem)products[i];
string message = "Are you sure you want to delete " + product.Description + "?";
DialogResult button =
MessageBox.Show(message, "Confirm Delete", MessageBoxButtons.YesNo);
if (button == DialogResult.Yes)
{
products.Remove(product);
InvItemDB.SaveItems(products);
FillItemListBox();
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
InvItemDB 代码:
public static class InvItemDB
{
private const string Path = @"..\..\InventoryItems.xml";
public static List<InvItem> GetItems()
{
// create the list
List<InvItem> items = new List<InvItem>();
// create the XmlReaderSettings object
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
// create the XmlReader object
XmlReader xmlIn = XmlReader.Create(Path, settings);
// read past all nodes to the first Book node
if (xmlIn.ReadToDescendant("Item"))
{
// create one Product object for each Product node
do
{
InvItem item = new InvItem();
xmlIn.ReadStartElement("Item");
item.ItemNo = Convert.ToString(xmlIn.ReadElementContentAsInt());
item.Description = xmlIn.ReadElementContentAsString();
item.Price = xmlIn.ReadElementContentAsDecimal();
items.Add(item);
}
while (xmlIn.ReadToNextSibling("Item"));
}
// close the XmlReader object
xmlIn.Close();
return items;
}
public static void SaveItems(List<InvItem> items)
{
// create the XmlWriterSettings object
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
// create the XmlWriter object
XmlWriter xmlOut = XmlWriter.Create(Path, settings);
// write the start of the document
xmlOut.WriteStartDocument();
xmlOut.WriteStartElement("Items");
// write each product object to the xml file
foreach (InvItem item in items)
{
xmlOut.WriteStartElement("Item");
xmlOut.WriteElementString("ItemNo", Convert.ToString(item.ItemNo));
xmlOut.WriteElementString("Description", item.Description);
xmlOut.WriteElementString("Price", Convert.ToString(item.Price));
xmlOut.WriteEndElement();
}
// write the end tag for the root element
xmlOut.WriteEndElement();
// close the xmlWriter object
xmlOut.Close();
}
xml项目列表:
<Items>
<Item>
<ItemNo>3245649</ItemNo>
<Description>Agapanthus</Description>
<Price>7.95</Price>
</Item>
<Item>
<ItemNo>3762592</ItemNo>
<Description>Limonium</Description>
<Price>6.95</Price>
</Item>
<Item>
<ItemNo>9210584</ItemNo>
<Description>Snail pellets</Description>
<Price>12.95</Price>
</Item>
<Item>
<ItemNo>4738459</ItemNo>
<Description>Japanese Red Maple</Description>
<Price>89.95</Price>
</Item>
</Items>
我只需要显示一个对话框,这样我就可以输入商品编号、描述和价格并将其保存到新列表中。
解决方案
下次请显示失败的实际代码。您的错误信息与您显示的代码不符;该代码中没有GetItems
方法,并且封闭类型不是InvItemDB
. 显示失败的代码,而不是无关的代码。
我的精神力量告诉我:
- 类型
InvItem
是内部的(请记住,对于非嵌套类型声明,如果您什么都不说,则默认可访问性是“内部的”。) - 方法
InvItemDB.GetItems()
——顺便提一下,它可能应该是一个只读属性——是公共类的公共方法。
这是不一致的;您不能在返回内部项目列表的公共类上使用公共方法。不允许调用者知道内部类型的存在!
要修复它,请删除不一致。要么GetItems
内部化,要么InvItem
公开化。
我已经完成并确保 InvItem 的类是公开的,但它仍然给我同样的错误
然后进行干净的重建,看看是否可以修复它。确保您没有两种类型都称为InvItem
.
更新:你说两个:
“我已经完成并确保 InvItem 类是公开的”
和
class InvItem { ... }
所以不,你没有“确保课程是公开的”。那需要是
public class InvItem { ... }
这个问题应该关闭并删除。
推荐阅读
- c# - Azure 服务的 .Net Core 异步方法
- google-maps - 在谷歌地图上显示用户位置
- scala - 如何使用延迟初始化对元组进行模式匹配?
- powershell - Powershell、RunAs 与凭据
- excel - 添加第三个条件来自动过滤 VBA
- r - 使用 gsub 提取文本
- openstack - 如何将 Openstack 与 glusterfs cinder volume 集成
- python - UnboundLocalError - 赋值前引用
- r - 在 R 中使用不涉及方程的 ~ (在 S4 类函数内)?
- react-native - 在本机反应中解析照片:// URI