-2

We are making a product invoice sheet - provides the item number, description, and price. I can't seem to get it to cooperate. I've gone through and made sure the class of InvItem is public but it's still giving me the same error code CS0050

 Inconsistent accessibility: return type 'List<InvIte>' is less accessible than method InvItemDB.GetItems()' 

I've gone through the code and made sure it matches my textbook and that all classes are public. This is only ONE part of my code --- it's not all 4 or 5 forms. If needed I can show those as well but this is the one that is giving the error message.

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

InvItem Code:

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

Main form:

  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 code:

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

The xml item list:

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

I just need a dialog to show so I can enter in the item number, description, and price and save it to a new list.

6
  • 4
    Please show us InvItemDB.GetItems and List. None of the code currently in the question appears relevant.
    – user47589
    Mar 27, 2019 at 14:35
  • 1
    We can't see your InvItem type declaration, the error says that you must make it public as well. Maybe you meant that method to be internal. Mar 27, 2019 at 14:38
  • 3
    shouldn't List<InvIte> be List<InvItem> ?
    – Livo
    Mar 27, 2019 at 14:41
  • 1
    Most probably you need to make InvIte class public. Or make the method InvItemDB.GetItems internal.
    – fhnaseer
    Mar 27, 2019 at 14:43
  • I missed an "m" while typing. It is InvItem
    – Amanda
    Mar 28, 2019 at 0:45

1 Answer 1

1

Next time please show the actual code that is failing. Your error message does not match the code you showed; there is no GetItems method in that code, and the enclosing type is not InvItemDB. Show the code that is failing, not unrelated code.

My psychic powers are telling me:

  • Type InvItem is internal (remember, the default accessibility if you say nothing is "internal" for a non-nested type declaration.)
  • Method InvItemDB.GetItems() -- which, incidentally, should probably be a read-only property -- is a public method of a public class.

That's an inconsistency; you cannot have a public method on a public class that returns a list of internal-only items. The caller is not allowed to know about the existence of an internal-only type!

To fix it, remove the inconsistency. Either make GetItems internal, or make InvItem public.

I've gone through and made sure the class of InvItem is public but it's still giving me the same error

Then do a clean rebuild and see if that fixes it. Make sure you do not have two types both called InvItem.


UPDATE: You said both:

"I've gone through and made sure the class of InvItem is public"

and

class InvItem { ... }

So no, you did not "make sure the class is public". That needs to be

public class InvItem { ... }

This question should be closed and deleted.

2
  • updated. Sorry, I had posted the one that was giving the error but it wasn't enough.
    – Amanda
    Mar 28, 2019 at 0:59
  • Again, the default is internal, not public. Mar 28, 2019 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.