none
Implementing interfaces RRS feed

  • Question

  • I've tried a few different ways to get this to work, but I can't.  The error messages that I'm getting say:
    "'SwissArmyKnife.SwissArmyKnife' does not implement interface member 'SwissArmyKnife.ICompass.compassPrice'"

    I'm also getting the same error for not implementing 'SwissArmyKnife.ICompass.compassWeight' 'SwissArmyKnife.IScissors.scissorPrice' and 'SwissArmyKnife.Iscissors.scissorWeight'  Please help!!!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace SwissArmyKnife
    {
        //This defines the first piece of the swiss army knife, the knife
        abstract class Knife
        {
            protected const float
                knifeWeight = 50f;
            protected double
                knifePrice = 5.00;

            public void Shine()
            {
                Console.WriteLine("The knife shines in the sunlight.");
            }
            public void Open()
            {
                Console.WriteLine("The knife pops open.");
            }
            public float Knife_Weight
            {
                get
                {
                    return knifeWeight;
                }
            }
            public double Knife_Price
            {
                get
                {
                    return Knife_Price;
                }
                set
                {
                    if (value < 0)
                        knifePrice = 0.00;
                    else
                        knifePrice = value;
                    Console.WriteLine("The price of the knife has been changed to: " + value);
                }
            }
        }
        //this defines the second piece of the swiss army knife, the compass.  It does it as an interface however so everything inside the interface must be left blank and filled in later
        interface ICompass
        {
            string Needle
            { get; }

            string Magnet
            { get; }

            double compassPrice
            { get; set; }

            float compassWeight
            { get; }

            void Direct();
        }
        //this is the same situation as the compass.
        interface IScissors
        {
            double scissorPrice
            { get; set; }

            float scissorWeight
            { get; set; }

            void Cut();
        }
        //This next bit creates a swiss army knife object using the knife object and the scissors and compass interfaces.  This is also where I will define the blank spots from the interfaces above
        class SwissArmyKnife : Knife, IScissors, ICompass
        {
            double scissorPrice,
                compassPrice,
                price;
            float
                scissorWeight,
                compassWeight,
                weight;
            public SwissArmyKnife()
            {
                Calc();
            }
            public void Calc()
            {
                weight = knifeWeight + scissorWeight + compassWeight;
                price = 4.00 + knifePrice + scissorPrice + compassPrice;
            }
            public void Cut()
            {
                Console.WriteLine("The scissors are out and you have cut something. I hope it wasn't important.");
            }
            public void Direct()
            {
                Console.WriteLine("The compass points you north.  Are you heading that way?");
            }
            public double CompassPrice
            {
                get
                {
                    return compassPrice;
                }
                set
                {
                    if (value < 0)
                        compassPrice = 0.00;
                    else
                        compassPrice = value;
                    Console.WriteLine("The price of the compass has been changed to: " + value);
                }
            }
            public double ScissorPrice
            {
                get
                {
                    return scissorPrice;
                }
                set
                {
                    if (value < 0)
                        scissorPrice = 0.00;
                    else
                        scissorPrice = value;
                    Console.WriteLine("The price of the scissors has been changed to: " + value);
                }
            }
            public double Price
            {
                get
                {
                    return price;
                }
                set
                {
                    if (value < 0)
                        price = 0.00;
                    else
                        price = value;
                    Console.WriteLine("The price of the Swiss Army Knife has been changed to: " + value);
                }
            }
            public float CompassWeight
            {
                get
                {
                    return compassWeight;
                }
                set
                { }
            }
            public float ScissorWeight
            {
                get
                {
                    return scissorWeight;
                }
                set
                { }
            }
            public float Weight
            {
                get
                {
                    return weight;
                }
            }
            public string Magnet
            {
                get
                {
                    return "This compass has a magnet.";
                }
            }
            public string Needle
            {
                get
                {
                    return "This compass has a needle.";
                }
            }
        }

    Wednesday, May 18, 2016 8:05 PM

Answers

  • In your interfaces, you define the properties (like compassPrice, for example) that start with lower case.

    But in your implementation, you define them with upper case (CompassPrice).

    Try changing your interface to have these properties be upper case.  Remember, C# is case-sensitive.

    Wednesday, May 18, 2016 9:29 PM