MarsBars9459
MarsBars9459

Reputation: 167

Setting a variable within an inherited class

I have a problem where I have two types of Topping classes, a normal "Topping" class, and one which inherits from Topping and adds a SpiceLevel, Called "SpicyTopping".

My problem is that whenever I construct a SpicyTopping object, the SpiceLevel is set to 0 and I have to manually set it after constructing the object

 //Objects
            Topping Mozzarella = new Topping("Mozzarella", 0.40f);
            Topping Ham = new Topping("Ham", 0.60f);
            Topping Eggs = new Topping("Eggs", 0.70f);
            Topping Salmon = new Topping("Salmon", 0.98f);
            Topping Tuna = new Topping("Tuna", 0.80f);
            Topping Garlic = new Topping("Garlic", 0.20f);
            Topping Beef = new Topping("Beef", 0.90f);
            Topping Chicken = new Topping("Chicken", 0.95f);
            SpicyTopping SweetChiliPeppers = new SpicyTopping("Sweet Chili Peppers", 0.31f, 1);//Mild
            SpicyTopping CayenneChiliPeppers = new SpicyTopping("Cayenne Chili Peppers", 0.32f, 2);//Moderate
            SpicyTopping HabaneroChiliPeppers = new SpicyTopping("Habanero Chili Peppers", 0.33f, 3);//Ferocious
            SweetChiliPeppers.ToppingSpiceLevel = 1;
            CayenneChiliPeppers.ToppingSpiceLevel = 2;
            HabaneroChiliPeppers.ToppingSpiceLevel = 3;

I don't understand why I have to do this as I think the constructor is set correctly

namespace AnotherTest
{
    public class Topping
    {
        //Private Variables
        private string toppingName;
        private float toppingPrice;

        //Properties
        //Topping Name
        public string ToppingName
        {
            get { return toppingName; }
            set { toppingName = value; }
        }

        //Topping Price
        public float ToppingPrice
        {
            get { return toppingPrice; }
            set { toppingPrice = value; }
        }

        //Constructor
        public Topping(string toppingName, float toppingPrice)
        {
            ToppingName = toppingName;
            ToppingPrice = toppingPrice;


        }
    }

    class SpicyTopping : Topping
    {
        //Private Variables
        private int toppingSpiceLevel;

        //Properties
        public int ToppingSpiceLevel
        {
            get { return toppingSpiceLevel; }
            set { toppingSpiceLevel = value; }
        }
        //Constructor
        public SpicyTopping(string toppingName,
                            float toppingPrice,
                            int ToppingSpiceLevel)
               : base(toppingName, toppingPrice)
        {
            ToppingName = toppingName;
            ToppingPrice = toppingPrice;
            ToppingSpiceLevel = toppingSpiceLevel;
        }
    }
}

What could the problem be? Thanks for the help

Upvotes: 1

Views: 62

Answers (2)

Darren
Darren

Reputation: 70728

In your constructor you have the variables defined incorrectly.

ToppingSpiceLevel = toppingSpiceLevel;

Should be:

toppingSpiceLevel = ToppingSpiceLevel;

The property ToppingSpiceLevel is what you are using to expose the topping spice level, however the private variable toppingSpiceLevel needs to be correctly set at the constructor level, as shown above.

Upvotes: 3

Skrrp
Skrrp

Reputation: 695

You've capitalised ToppingSpiceLevel in your constructor definition.

Upvotes: 0

Related Questions