Reputation: 160
I am still working on my infamous virtual fast food restaurant console application. I have many classes that represent different food items. These food items will be stored into different Menus
and after I create what we have on our Menu
I then have to present it to the user. The user will select items and then the cost of these items will be added to an instance of class Order
where an OrderTotal()
method will calculate the final cost of every item selected and after payment is processed by either selecting Credit or Debit in the SelectPayment(Payment payment)
and ProcessPayment(Payment payment)
methods. So here in this exact snippet, I am working with Fries. Unlike Burgers there are only one type of Frenchfries and that is our House Fries. So instead of List<List<Fries>>
it is just List<Fries>
because the one instance of Fries will not change but the user will be able to select a set of enum
values which will dictate how to proceed with the Fries being large, small medium, and available with salt. So the gist is, in Program.cs, to be able to FrenchFries.Add(*fries*)
and then it will add to the private
List
of Fries
I have in my Fries
class
The same is also true of Chicken Burgers and any other kind of food that needs to be added to a menu and presented to a user. The Visual Studio error is Inconsistent accessibility: parameter type: 'DPBurgers.Fries' is less accessible than method 'DPBurgers.Menu.AddFries(DPBurgers.Fries)'
so is it an issue of the way I'm trying to add them to the private list? Or is it a problem with parameters?
Contents of the main program file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
///<summary>
///Simple C# program that allows a user to browse a menu
/// at a fast food restraunt and then make selections on what
/// to eat based on those choices. This program is designed to
/// excersize the basics of C# fundamentals.
/// Data access will be class based. No Databases or serialized formats
/// will be used.
/// </summary>
namespace DPBurgers
{
class Program
{
static void Main(string[] args)
{
string filler = "*****************";
Console.WriteLine("DPBURGERS..\nDerek Pauley..\n{0}", DateTime.Now);
Console.WriteLine(filler);
Console.WriteLine(filler);
Console.WriteLine("Welcome To DP Burgers. Please review our menu and let me know when you're ready to order by typing 'Ready'. ");
Console.WriteLine(filler);
//List of our burgers.
var burger1 = new Burger
{
Name = "Double Decker",
Price = 5.00m,
};
var burger2 = new Burger
{
Name = "Single Stack",
Price = 3.00m,
};
var burger3 = new Burger
{
Name = "Veggie Burger",
Price = 2.00m,
};
var kidsBurger = new Burger
{
Name = "Kiddie Burger",
Price = 1.00m,
};
var valueBurger = new Burger
{
Name = "DPValue Burger",
Price = 1.00m,
};
//List of our chicken sandwiches
var DpHouseChickenSandwich = new Chicken();
DpHouseChickenSandwich.Name = "House Chicken Sandwich";
DpHouseChickenSandwich.Price = 5.00m;
var answer = Console.ReadLine();
if (answer == "yes")
{
DpHouseChickenSandwich.SelectChickenCook();
}
//Only one kind of fries - House Fries
var FrenchFries = new Fries();
FrenchFries.Name = "House Fries";
List<Burger> DpBurgerMenu = new List<Burger>();
DpBurgerMenu.Add(burger1);
DpBurgerMenu.Add(burger2);
DpBurgerMenu.Add(burger3);
List<Burger> DpAdditionalBurgers = new List<Burger>();
DpAdditionalBurgers.Add(kidsBurger);
DpAdditionalBurgers.Add(valueBurger);
Menu DPBurgerMainMenu = new Menu();
DPBurgerMainMenu.Add(DpBurgerMenu);
DPBurgerMainMenu.Add(DpAdditionalBurgers);
Menu DPFries = new Menu();
DPFries.AddFries(FrenchFries);
{
}
}
}
}
//if user orders fries then use selectSize method
Contents of the Fries class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DPBurgers
{
public enum FrySize { Small, Medium, Large}
public enum FrySalt { Yes, No}
class Fries : Food
{
public void SelectSize()
{
FrySize small = FrySize.Small;
FrySize medium = FrySize.Medium;
FrySize large = FrySize.Large;
decimal smallFryPrice = 1.00m;
decimal medFryPrice = 2.00m;
decimal largeFryPrice = 3.00m;
Console.WriteLine("And what size fries would you like?. Type s for small, m for medium, or l for large.");
var sizeRequest = Console.ReadLine();
try
{
switch (sizeRequest)
{
case "s":
Console.WriteLine("Alright, one {0} fry coming right up", small); //prices from the selected fries will be added to the total
Price = smallFryPrice;
withSalt();
break;
case "m":
Console.WriteLine("Alright one {0} fry coming right up", medium);
Price = medFryPrice;
withSalt();
break;
case "l":
Console.WriteLine("Alright one {0} fry coming right up", large);
Price = largeFryPrice;
withSalt();
break;
default:
Console.WriteLine("Please make a valid selection");
SelectSize();
break;
}
}catch(FormatException feX)
{
Console.WriteLine("{0}", feX);
}
}
public void withSalt()
{
try
{
FrySalt yes = FrySalt.Yes;
FrySalt no = FrySalt.No;
string takeSalt = "yes";
string noSalt = "no";
Console.WriteLine("And would you like salt with that? Please type Yes or No.");
var saltReply = Console.ReadLine();
if (saltReply != null && saltReply == yes.ToString() || saltReply == takeSalt)
{
Console.WriteLine("Alright thanks!");
}
else if (saltReply != null && saltReply == no.ToString() || saltReply == noSalt)
{
Console.WriteLine("Alright no problem!");
}
else if (saltReply == null || saltReply != yes.ToString() && saltReply != no.ToString() && saltReply != takeSalt
&& saltReply != noSalt && saltReply is string)
{
Console.WriteLine("Please enter acceptable answer.");
}
else
{
Console.WriteLine("You are entering information in an unacceptable format");
}
} catch(FormatException fEx)
{
Console.WriteLine("{0}",fEx);
}
}
}
}
Upvotes: 0
Views: 274
Reputation: 10229
The error gives it away:
Inconsistent accessibility: parameter type: 'DPBurgers.Fries' is less accessible than method 'DPBurgers.Menu.AddFries(DPBurgers.Fries)'
Your problem is the accessibility of the different classes. You have a public class that accepts a parameter that is less accessible. Ie: protected, internal or private.
When not specifying an accessibility modifier, internal
is assumed: class Fries : Food
. Change that to public class Fries : Food
and all will be well.
OO: You can make the base class public and have the different derived classes less accessible (for whatever reason) and as long as your parameters always expect the base class, it will work.
Upvotes: 3
Reputation: 152566
Since you didn't post the code for Menu
I'm assuming that Menu.AddFries
is marked as public
, but the Fries
class you use as a parameter is not. Classes are internal
by default unless you specify that they are public
. Just make your Fries
class public
:
public class Fries : Food
Note that Food
will then need to be public
as well.
Upvotes: 2
Reputation: 26068
The error is telling you the problem: Inconsistent accessibility: parameter type: 'DPBurgers.Fries' is less accessible than method 'DPBurgers.Menu.AddFries(DPBurgers.Fries)'
You have a function called AddFries
that is public, but it takes as a parameter a type that is not public, so you could get into a situation where something is able to call the method, but unable to know the type of parameter to send to the method. Make the function definition and class being sent the same visibility (public / private / internal... whichever makes sense for you) and it will eliminate this error.
(Also note the lack of specifying a visibility modifier on a class defaults to internal)
So the quick fix is either change your methods to be internal rather than public, or change your classes to be public.
Upvotes: 3