Home > Software engineering >  CS0051: Parameter type 'abstract class' is less accessible than method 'method'
CS0051: Parameter type 'abstract class' is less accessible than method 'method'

Time:12-12

I've been working with abstract classes for about a month and I'm not too familiar with debugging their errors. My error message reads "Inconsistent accessibility: parameter type 'ConcessionItem' is less accessible than method 'Person.AddConcessionItem(ConcessionItem)'". I believe the error is being thrown due to using abstract classes and interfaces but I don't think I've ever solved an error like this before.

Person.cs

public class Person
    {
        public decimal Cash;

        public string Name;

        public string PreferredConcessionSize;

        public Ticket Ticket;

        private List<ConcessionItem> concessionItems;

        public Person()
        {
            this.concessionItems = new List<ConcessionItem>();
        }

        public void AddConcessionItem(ConcessionItem item)
        {
            this.concessionItems.Add(item);
        }
    }

ConcessionItem.cs

abstract class ConcessionItem : IPurchasable
    {
        private string size;

        private decimal price;

        private int servingsRemaining;

        public ConcessionItem(string size)
        {
            this.Size = size;

            this.Initialize();
        }
    }

CodePudding user response:

There is no relation to abstract classes.

In your code, class Person is public, while class ConcessionItem does not have an explicit access modifier. In that case, compiler implicitly uses private. In the end, you have a public AddConcessionItem which accepts a parameter of private class ConcessionItem

CodePudding user response:

You have ConcessionItem.cs as default private. You would need to make it public to make it the same as Person.cs.

  • Related